如何在Mac OSX下使用gcc设置可执行文件的运行时路径(-rpath)? [英] How to set the runtime path (-rpath) of an executable with gcc under Mac OSX?

查看:642
本文介绍了如何在Mac OSX下使用gcc设置可执行文件的运行时路径(-rpath)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mac OSX下设置可执行文件(链接器)的运行时路径,以便在程序启动时动态链接器找到非标准位置的共享库。



在Linux下, -Xlinker -rpath -Xlinker / path / to (或者使用 -Wl, -rpath,/ path / to ),并且在Solaris下,您可以在编译器命令行中添加 -R / path / to



我发现某些信息表示Mac OS X gcc自10.5版本开始支持-rpath支持,即自2008年以来。



我试图用一个简单的例子来工作 - 没有成功:

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

和:

  $ cat main.c 

#include< stdio.h>

int blah(int);

int main()
{
printf(%d\\\
,blah(22));
返回0;

编译成这样:

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

现在测试:

  $ mkdir t 
$ mv libblah.dylib t
$ ./a.out
dyld:Library未加载:libblah.dylib
引用自:/Users/max/test/./a.out
原因:找不到图像
跟踪/ BPT陷阱

$ p
$ b

因此,问题:如何在Mac OSX中设置链接器的运行时路径?



Btw,设置 DYLD_LIBRARY_PATH 可以工作 - 但我不想使用这个黑客。

strong>编辑:关于 otool -L

  $ otool -L a.out 
a.out:
libblah.dylib(兼容版本0.0.0,当前版本0.0.0)
/usr/lib/libSystem.B.dylib(兼容性版本1.0.0,当前版本125.2.1)

看来 otool -L 仅打印可执行文件链接的库名称(可能位于链接时的位置),并且没有运行时路径信息。

通过实验找到并检查由Xcode生成的命令行,获取引用rpath Dave Driblin演示项目

otool -L 显示链接库的安装名称。要使 @rpath 正常工作,您需要更改库的安装名称:

  $ 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`


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\n", 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`

这篇关于如何在Mac OSX下使用gcc设置可执行文件的运行时路径(-rpath)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆