bash 加载共享库时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7455601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error while loading shared libraries
提问by ulyssis2
Thank you guys answering my previous problem on undefined reference to function. As you suggested, the reason under the problem is not linking the libraries. Now I have generated the executable file with: (the version of my g++ and gcc is 4.4.5. I am using Ubuntu 10.10.)
谢谢你们回答我之前关于undefined reference to function 的问题。正如您所建议的,问题下的原因不是链接库。现在我已经生成了可执行文件:(我的 g++ 和 gcc 的版本是 4.4.5。我使用的是 Ubuntu 10.10。)
g++ -o ex_addinst ./ex_addinst.o -L/home/li/work/tools/lindo/lindoapi/bin/linux64 -m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc
But there comes another problem, when I run the executable file with
但是还有另一个问题,当我运行可执行文件时
./ex_addinst
errors appear: (I am not sure I should start a new problem or not currently....)
出现错误:(我不确定我现在是否应该开始一个新问题....)
./ex_addinst: error while loading shared libraries: liblindo64.so.6.0: cannot open shared object file: No such file or directory
But liblindo64.so.6.0 exists in the folder of the lib ~/lindoapi/bin/linux64 which contains following files:
但是 liblindo64.so.6.0 存在于 lib ~/lindoapi/bin/linux64 的文件夹中,其中包含以下文件:
libconsub3.so libirc.so liblindojni.so libmosek64.so.5.0 lindo.par
libguide.so liblindo64.so liblindojni.so.6.0.3 libsvml.so placeholder
libimf.so liblindo64.so.6.0 libmosek64.so lindoapivars.sh runlindo
I have created a symbolic link between liblindo.so.6.0 and liblindo.so:
我在 liblindo.so.6.0 和 liblindo.so 之间创建了一个符号链接:
ln -sf liblindo64.so.6.0 liblindo64.so
There is '-llindo64' is the g++ command, so I thought liblindo64.so.6.0should have been linked. I have tried to change -L to -Llib, but doesn't help.
'-llindo64' 是 g++ 命令,所以我认为 应该链接liblindo64.so.6.0。我试图将 -L 更改为 -Llib,但没有帮助。
Can anyone tell me what is wrong here? Thanks!
谁能告诉我这里有什么问题?谢谢!
采纳答案by Mat
You need to have the directory where the .sofiles live in in runtime linker's search path.
您需要.so在运行时链接程序的搜索路径中包含文件所在的目录。
You can do that by changing the LD_LIBRARY_PATHenvironment variable like this:
您可以通过LD_LIBRARY_PATH像这样更改环境变量来做到这一点:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HOME/lindoapi/bin/linux64
before starting your executable.
在启动可执行文件之前。
回答by Employed Russian
If you are not going to install libraries currently under /home/li/work/tools/lindo/lindoapi/bin/linux64into a system directory (/usr/lib, /usr/local/lib, etc.), then it is better to simply link the application such that it will just work(TM):
如果您不打算将当前的库安装/home/li/work/tools/lindo/lindoapi/bin/linux64到系统目录(/usr/lib,/usr/local/lib等)中,那么最好简单地链接应用程序,使其正常工作(TM):
gcc -o ex_addinst ./ex_addinst.o \
-L/home/li/work/tools/lindo/lindoapi/bin/linux64 \
-Wl,-rpath=/home/li/work/tools/lindo/lindoapi/bin/linux64 \
-m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl \
-lm -lguide -lpthread -lsvml -limf -lirc
This is preferable to always having to set LD_LIBRARY_PATH, because
这比总是必须设置更可取LD_LIBRARY_PATH,因为
- other people can run your executable (without having to set
LD_LIBRARY_PATH) and, - it doesn't slow down all the other applications (otherwise they will all search
LD_LIBRARY_PATHforlibc.so.6, etc.)
- 其他人可以运行您的可执行文件(无需设置
LD_LIBRARY_PATH),并且, - 它不会所有其他应用程序变慢(否则他们将所有的搜索
LD_LIBRARY_PATH对libc.so.6等)
The reason your LD_LIBRARY_PATHsetting didn't work (comment to Mat's answer) is that you used HOMEwhere /homewas intended.
您的LD_LIBRARY_PATH设置不起作用的原因(评论 Mat 的答案)是您HOME在/home预期的地方使用了。
回答by ulyssis2
to sum the solution:
总结解决方案:
I add the path to ~./bashrc with:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HOME/.../lindoapi/bin/linux64
(after generating .o file)link the objective file with:
g++ -o ex_addinst ./ex_addinst.o -L/home/.../lindoapi/bin/linux64 -m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc
我将路径添加到 ~./bashrc :
导出 LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HOME/.../lindoapi/bin/linux64
(生成 .o 文件后)将目标文件与:
g++ -o ex_addinst ./ex_addinst.o -L/home/.../lindoapi/bin/linux64 -m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc

