如何在dylib中指定rpath? [英] How can I specify the rpath in a dylib?

查看:360
本文介绍了如何在dylib中指定rpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库: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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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