English 中文(简体)
How to set the runtime path (-rpath) of an executable with gcc under Mac OSX?
原标题:

I want to set under Mac OSX the runtime path of an executable (for the linker) at compile time, such that shared libraries at non-standard locations are found by the dynamic linker at program start.

Under Linux this is possible with -Xlinker -rpath -Xlinker /path/to (or using -Wl,-rpath,/path/to) and under Solaris you can add -R/path/to to the compiler command line.

I found some information that Mac OS X gcc has -rpath support since 10.5, i.e. since ~ 2008.

I tried to get it working with a minimal example - without success:

$ cat blah.c 
int blah(int b)
{
  return b+1;
}

And:

$ cat main.c 

#include <stdio.h>

int blah(int);

int main ()
{
  printf("%d
", blah(22));
  return 0;
}

Compiled it like this:

$ gcc -c  blah.c
$ gcc -dynamiclib blah.o -o libblah.dylib
$ gcc main.c -lblah -L`pwd`  -Xlinker -rpath -Xlinker `pwd`/t

Now the test:

$ mkdir t
$ mv libblah.dylib t
$ ./a.out
dyld: Library not loaded: libblah.dylib
  Referenced from: /Users/max/test/./a.out
  Reason: image not found
Trace/BPT trap

Thus the question: How to I set the runtime path for the linker under Mac OSX?

Btw, setting DYLD_LIBRARY_PATH works - but I don t want to use this hack.

Edit: Regarding otool -L:

$ otool -L a.out 
a.out:
        libblah.dylib (compatibility version 0.0.0, current version 0.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)

It seems that otool -L only prints the library names (and probable the locations at link time) the executable was linked against and no runtime path information.

最佳回答

Found by experimentation, and inspecting the command lines generated by Xcode for a reference rpath demo project by Dave Driblin:

otool -L shows you the install name of the linked libraries. To get @rpath to work, you need to change the install name of the library:

$ gcc -dynamiclib blah.o -install_name @rpath/t/libblah.dylib -o libblah.dylib
$ mkdir t ; mv libblah.dylib t/
$ gcc main.c -lblah -L`pwd`/t -Xlinker -rpath -Xlinker `pwd`
问题回答

暂无回答




相关问题
gcc -fPIC seems to muck with optimization flags

Following along from this question: how-do-i-check-if-gcc-is-performing-tail-recursion-optimization, I noticed that using gcc with -fPIC seems to destroy this optimization. I am creating a shared ...

Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Getting rid of pre-compiled headers

OK, I have old Metrowerks code for Mac and Windows where the previous developer used pre-compiled headers for every project that this code base builds. How does one get rid of Pre-compiled headers, ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

How to compile for Mac OS X 10.5

I d like to compile my application for version 10.5 and forward. Ever since I upgraded to Snow Leopard and installed the latest XCode, gcc defaults to 10.6. I ve tried -isysroot /Developer/SDKs/...

热门标签