如何使用相对路径从共享库链接到共享库?
我正在开发一个 Firefox 插件,它使用外部库在浏览器上渲染 3D 图形。
问题是我希望插件使用包含它的外部库而不更改 LD_LIBRARY_PATH 变量。
这些库安装在相对于插件的位置(也是共享库),而实际的可执行文件(即浏览器)可以完全位于其他位置。
我正在 Ubuntu 上测试(Windows 版本的插件没有问题) 我的依赖项是 OpenSceneGraph 库,静态编译将使插件变得非常大(如果有另一个则不是一个选项)。
I'm working on a Firefox plugin that uses external libraries to render 3D graphics on the browser.
The problem is that I want the plugin to use external libraries packed with it without changing the LD_LIBRARY_PATH
variable.
The libraries are installed in a position relative to the plugin (a shared library too), while the actual executable (i.e. the browser) can be located somewhere entirely else.
I'm testing it on Ubuntu (no problem at Windows version of the plugin)
My dependencies are OpenSceneGraph libraries and static compilation will make the plugin really big (not an option if there is another one).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
链接时使用 rpath 选项并指定“特殊”路径 $ORIGIN。
示例:
这是一个详细说明如何使用 $ORIGIN 的网站:
http://www.itee.uq.edu.au/~daniel/using_origin/
Use the rpath option when linking and specify the 'special' path $ORIGIN.
Example:
Here's a site that elaborates on using $ORIGIN:
http://www.itee.uq.edu.au/~daniel/using_origin/
您可以在编译期间使用
-L
标志来指定链接器可以找到共享对象的相对路径。如果您已经生成了 lib,则可以通过直接调用 ld 命令进行链接。
提示:您可以使用unix命令
nm
轻松检查lib中是否定义了某些符号。这是检查链接是否良好的有用方法。(如果我是你,我只会像你在帖子中所说的那样临时更改
LD_LIBRARY_PATH
。你为什么不想这样做呢?)You could maybe use the
-L
flag during the compilation to specify the relative path where the linker can find your shared objects.If you have already generated your lib, you can link by directly invoking the
ld
command.Tips : You can easily check if some symbols are defined in a lib using the unix command
nm
. This is a useful way to check that the linking is well-done.(If I were you, I would just change temporaly the
LD_LIBRARY_PATH
as you said in your post. Why don't you want to do this ?)出于安全原因使用相对 rpath 是错误的,
您应该使用 libdl 函数(dlopen 等)
It's wrong to use relative rpath for security reason,
You should use libdl functions (dlopen, etc)