如何在dylib中指定rpath?

编程入门 行业动态 更新时间:2024-10-25 01:33:02
本文介绍了如何在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?

本文发布于:2023-11-27 21:00:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   dylib   rpath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!