我正在尝试将我的项目链接到放置在项目的基本目录[proj_dir] / lib上的一组特定的自定义编译库 - 而不是在任何系统的/ lib,/ usr / lib或/ usr / local /上lib - 避免与相同库的已安装库存版本混淆。
我可以通过使用-L标志传递库路径来编译项目,但是error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory运行已编译的二进制error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory时error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory ,并且ldd告诉我它找不到那些特定的库。
另一方面,如果我将LD_LIBRARY_PATH=[proj_dir]/lib传递给可执行文件,我可以毫无问题地运行它。 还有,有没有办法隐式链接这些库,而不必在运行时手动设置LD_LIBRARY_PATH ?
I'm trying to link a project of mine to a particular set of custom-compiled libraries placed on the project's base directory [proj_dir]/lib - not on any of the system's /lib, /usr/lib or /usr/local/lib - to avoid conficts with the installed stock versions of those same libraries.
I'm able to compile the project by passing the library path with the -L flag, but I get error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory when I run the compiled binary, and ldd tells me it can't find those particular libs.
On the other hand, I am able to run it without issue if I pass LD_LIBRARY_PATH=[proj_dir]/lib to the executable. Still, is there a way to link those libraries implicitly, without having to manually set the LD_LIBRARY_PATH at runtime?
最满意答案
你也可以
在调用实际程序之前编写一个包装脚本以始终包含LD_LIBRARY_PATH (更灵活)。 将-Wl,-rpath=<directory>添加到链接器选项,以将目录添加到运行时库搜索路径。 所以假设你有一个libfoo.so并且你的程序和DSO应该位于同一个目录中,你的编译命令可能如下所示: gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN' gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN' 。更新:正如Maxim正确指出的那样,设置-rpath=. 是危险的,应该避免 。
对于-Wl,请参阅gcc联机帮助页 ,有关-rpath请参阅ld联机帮助页 。
You can either
Write a wrapper script to always include LD_LIBRARY_PATH before calling the actual program (more flexible). Add -Wl,-rpath=<directory> to your linker options to add a directory to the runtime library search path. So assuming you have a libfoo.so and your program and DSO should lie in the same directory, your compilation command could look like this: gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN'.Update: As correctly noted by Maxim, setting -rpath=. is dangerous and should be avoided.
For -Wl,, see the gcc manpage and for -rpath see the ld manpage.
更多推荐
发布评论