我有一个库:libfoo.dylib.该问题在以下命令中得到说明:
I have a library: libfoo.dylib. The problem is illustrated in the commands:
$ install_name_tool -id "@rpath/libfoo.dylib" libfoo.dylib $ install_name_tool -add_rpath "@executable_path/" libfoo.dylib $ gcc -o foo foo.c -lfoo $ ./foo #<==== I want this to work dyld: Library not loaded: @rpath/libfoo.dylib Referenced from: ~/./foo Reason: image not found $ install_name_tool -add_rpath "@executable_path/" foo #<=== I dont want to have to specify here where to look for the library $ ./foo Hello World我如何达到不必在可执行编译中指定库在哪里的目标?
How do I achieve the goal of not having to specify at executable compile where the library is?
推荐答案我必须承认,对于您要实现的目标我有些困惑.使用运行路径搜索路径的全部目的是,加载库的图像定义了加载库时要使用的搜索路径.您要的是让库定义可执行文件在哪里找到.通过简单地将dylib的安装名称设置为适当的值,就可以在不使用运行路径搜索路径的情况下完成此操作.根据您的特定示例,听起来您想将安装名称设置为类似@loader_path/libfoo.dylib的名称.考虑以下内容,这些内容与示例相同:
I must confess that I'm a little confused as to what you're trying to achieve. The entire point of using the runpath search path is that the images loading the library define the search path to be used when loading the library. What you're asking for is for the library to define where the executable should find it. That can be accomplished without using the runpath search path by simply setting the install name of the dylib to the appropriate value. Based on your particular example, it sounds like you want to set the install name to something like @loader_path/libfoo.dylib. Consider the following, which is along the same lines of your sample:
$ cat a.c int a(void) { return 1; } $ cc -install_name "@loader_path/liba.dylib" -dynamiclib -o liba.dylib a.c $ cat main.c #include <stdio.h> extern int a(void); int main(int argc, char **argv) { fprintf(stderr, "A: %d\n", a()); return 0; } $ cc -L. -la -o main main.c $ ./main A: 1 $该库通过设置其安装名称来告诉与其链接的可执行文件如何找到它,并且在链接可执行文件以使其在运行时找到该库时,无需执行任何特殊操作.
The library tells executables that link against it how to find it by setting its install name, and nothing special needs to be done when linking the executable to have it find the library at runtime.
更多推荐
如何在dylib中指定rpath?
发布评论