共享库路径为可执行目录 [英] Shared Library Path as Executable Directory

查看:248
本文介绍了共享库路径为可执行目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序被分成几个库,用于代码重用。在Windows上,我所要做的是将.dll文件放在与可执行文件相同的路径,它会自动找到它们。在Linux上(因为它硬编码路径)我必须指定环境变量 LD_LIBRARY_PATH 或在可执行文件之前预加载库。

I have an application that is broken into several libraries for purposes of code reuse. On Windows all I have to do is put the .dll files in the same path as the executable and it automatically finds them. On Linux (since it hardcodes the paths to things) I have to specify the environmental variable LD_LIBRARY_PATH or preload the libraries before the executable.

我已经看到了一些使用 -Wl,-rpath =< PATH> 链接器选项嵌入路径的事情, 作为路径。但这只是在当前工作目录,而不是可执行文件的目录。

I've seen some things about embedding the path using the linker option of -Wl,-rpath=<PATH> and I've tried it using . as the path. But that just looks in the current working directory, not the executable's directory.

有一种方法来指定在链接器中查看可执行文件的目录共享库默认情况下(如在Windows上)?

Is there a way to specify in the linker to look in the directory of the executable for the shared libraries by default (like on Windows)?

谢谢!
Matt

Thanks! Matt

推荐答案

通过ld或其他达尔文工具的适当选项,您的RPATH需要$ ORIGIN。请参见

You need $ORIGIN in your RPATH, via an appropriate option to ld or other Darwin tool. See this and this.

请记住,$必须真的在路径中结束,因此您需要在链接命令行中引用或转义它。

Remember that the $ has to really end up in the path, so you need to quote or escape it in the link command line.

更新:
您可以看到链接器实际上放在可执行文件中, p>

Update: You can see what the linker actually put into your executable with

readelf -d /path/to/exe | grep RPATH

这里是输出应该是什么样子:

Here is what the output should look like:

 0x0000000f (RPATH)              Library rpath: [$ORIGIN]

这篇关于共享库路径为可执行目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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