为什么每次运行应用程序时都必须使用导出来定义 LD_LIBRARY_PATH?
我有一些使用一些共享库的代码(gcc 上的 c 代码)。 编译时,我必须使用 -I 和 -L 显式定义包含目录和库目录,因为它们不在标准位置。 当我尝试运行代码时,出现以下错误:
./sync_test
./sync_test: error while loading shared libraries: libsync.so: cannot open shared object file: No such file or directory
但是,执行以下操作,一切正常:
export LD_LIBRARY_PATH="/path/to/library/"
./sync_test
现在,奇怪的部分是,这只有效一次。 如果我尝试再次运行sync_test,除非我先运行导出命令,否则我会收到相同的错误。 我尝试将以下内容添加到我的 .bashrc 中,但没有什么区别:
LD_LIBRARY_PATH="/path/to/library/"
I have some code that uses some shared libraries (c code on gcc). When compiling I have to explicitly define the include and library directories using -I and -L, since they aren't in the standard places. When I try to run the code, I get the following error:
./sync_test
./sync_test: error while loading shared libraries: libsync.so: cannot open shared object file: No such file or directory
However, do the following, everything works just fine:
export LD_LIBRARY_PATH="/path/to/library/"
./sync_test
Now, the strange part is, this only works once. If I try and run sync_test again I get the same error unless I run the export command first. I tried adding the following to my .bashrc, but it made no difference:
LD_LIBRARY_PATH="/path/to/library/"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你在 .bashrc 中“导出”了吗?
Did you 'export' in your .bashrc?
您可以使用
rpath
将其烘焙到二进制文件本身中,而不是在运行时使用 LD_LIBRARY_PATH 覆盖库搜索路径。 如果您与 GCC 链接,添加-Wl,-rpath,
应该可以解决问题,如果您与 ld 链接,则只需-rpath
。Instead of overriding the library search path at runtime with LD_LIBRARY_PATH, you could instead bake it into the binary itself with
rpath
. If you link with GCC adding-Wl,-rpath,<libdir>
should do the trick, if you link with ld it's just-rpath <libdir>
.如果您在系统上安装了该软件,您还可以做的是将包含共享库的目录添加到您的 /etc/ld.so.conf 文件中,或者在以下位置创建一个新文件: /etc/ld.so.conf.d/
(我都检查过 RHEL5 和 Ubuntu 发行版,所以我认为它对于 Linux 是通用的)
ldconfig 程序将确保它们在系统范围内包含。
请参阅以下链接了解更多信息:
www.dwheeler.com/secure-programs/Secure-Programs- HOWTO/dlls.html
What you also can do, if it's something you installed on your system, is to add the directory that contains the shared libraries to your /etc/ld.so.conf file, or make a new file in /etc/ld.so.conf.d/
(I've both checked RHEL5 and Ubuntu distribution so I think it's generic for linux)
The ldconfig program will make sure they are system-wide included.
See the following link for more information:
www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/dlls.html
您可以在代码中添加具有新定义的调用系统:
但是,我不知道这是正确的解决方案,但它有效。
问候
You could add in your code a call system with the new definition:
But, I don't know it that is the rigth solution but it works.
Regards
您应该避免在
.bashrc
中设置LD_LIBRARY_PATH
。 有关详细信息,请参阅“为什么 LD_LIBRARY_PATH 不好
”。链接时使用链接器选项 -rpath ,以便动态链接器知道在运行时在哪里找到
libsync.so
。编辑:
另一种方法是使用这样的包装器
如果
sync_test
启动任何其他程序,它们最终可能会使用/path/to/library
中的库,这可能或可能不是有意的。You should avoid setting
LD_LIBRARY_PATH
in your.bashrc
. See "Why LD_LIBRARY_PATH is bad
" for more information.Use the linker option -rpath while linking so that the dynamic linker knows where to find
libsync.so
during runtime.EDIT:
Another way would be to use a wrapper like this
If
sync_test
starts any other programs, they might end up using the libs in/path/to/library
which may or may not be intended.在你的 .bashrc 中使用
,否则,它只能用于 bash 而不能用于你启动的任何程序。
链接时尝试 -R/path/to/library/ 标志,它会让程序在该目录中查找,并且不需要设置任何环境变量。
编辑:看起来
-R
仅适用于 Solaris,而您使用的是 Linux。另一种方法是将路径添加到
/etc/ld.so.conf
并运行ldconfig
。 请注意,这是一个全局更改,将应用于所有动态链接的二进制文件。Use
in your .bashrc otherwise, it'll only be available to bash and not any programs you start.
Try
-R/path/to/library/
flag when you're linking, it'll make the program look in that directory and you won't need to set any environment variables.EDIT: Looks like
-R
is Solaris only, and you're on Linux.An alternate way would be to add the path to
/etc/ld.so.conf
and runldconfig
. Note that this is a global change that will apply to all dynamically linked binaries.你可以把这一切放在一行上:
应该让事情变得更容易,即使它不会改变任何根本性的东西
You can just put this all on one line:
Should make things a little easier, even if it doesn't change anything fundamental