为什么每次运行应用程序时都必须使用导出定义 LD_LIBRARY_PATH? [英] Why do I have to define LD_LIBRARY_PATH with an export every time I run my application?
问题描述
我有一些代码使用了一些共享库(gcc 上的 c 代码).编译时,我必须使用 -I 和 -L 显式定义包含和库目录,因为它们不在标准位置.当我尝试运行代码时,出现以下错误:
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
现在,奇怪的是,这只起作用一次.如果我再次尝试运行 sync_test,除非我先运行 export 命令,否则我会得到同样的错误.我尝试将以下内容添加到我的 .bashrc 中,但没有任何区别:
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/"
推荐答案
使用
export LD_LIBRARY_PATH="/path/to/library/"
否则,在您的 .bashrc 中,它只能用于 bash 而不能用于您启动的任何程序.
in your .bashrc otherwise, it'll only be available to bash and not any programs you start.
在链接时尝试使用 -R/path/to/library/
标志,它会使程序在该目录中查找,您无需设置任何环境变量.
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.
看起来 -R
仅适用于 Solaris,而您使用的是 Linux.
Looks like -R
is Solaris only, and you're on Linux.
另一种方法是将路径添加到 /etc/ld.so.conf
并运行 ldconfig
.请注意,这是一项适用于所有动态链接二进制文件的全局更改.
An alternate way would be to add the path to /etc/ld.so.conf
and run ldconfig
. Note that this is a global change that will apply to all dynamically linked binaries.
这篇关于为什么每次运行应用程序时都必须使用导出定义 LD_LIBRARY_PATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!