How to link to shared lib from shared lib with relative path?

不打扰是莪最后的温柔 提交于 2019-12-03 18:46:12

问题


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).


回答1:


Use the rpath option when linking and specify the 'special' path $ORIGIN.

Example:

-Wl,-R,'$ORIGIN/../lib'

Here's a site that elaborates on using $ORIGIN: http://www.itee.uq.edu.au/~daniel/using_origin/




回答2:


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 ldcommand.

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 ?)




回答3:


It's wrong to use relative rpath for security reason,

You should use libdl functions (dlopen, etc)



来源:https://stackoverflow.com/questions/3960641/how-to-link-to-shared-lib-from-shared-lib-with-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!