LD_LIBRARY_PATH,linux中共享的lib路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9412296/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:46:01  来源:igfitidea点击:

LD_LIBRARY_PATH, the shared lib path in linux

clinuxshared-libraries

提问by Alcott

I wrote a shared object, say libsd.so, and I put libsd.soand its header file sd.hin ~/lib.

我写了一个共享对象,比如说libsd.so,我把libsd.so它的头文件sd.h放在~/lib.

Here is another program using libsd.so, say test.c, then compile it like this:

这是另一个使用libsd.so,比如说 的程序test.c,然后像这样编译它:

$ gcc -o test test.c -I~/lib -L~/lib -lsd

Then I run testlike this:

然后我test像这样运行:

$ ./test
./test_sd: error while loading shared libraries: libsd.so: cannot open shared object file: No such file or directory

So I set export LD_LIBRARY_PATH=., then it works. But if I unset LD_LIBRARY_PATHand put LD_LIBRARY_PATH=~/libin my ~/.bashrc, then source ~/.bashrc, again it doesn't work for ./test, WHY?

所以我设置了export LD_LIBRARY_PATH=.,然后它就起作用了。但是,如果我unset LD_LIBRARY_PATH放入LD_LIBRARY_PATH=~/lib我的~/.bashrc,那么source ~/.bashrc,它又对 不起作用./test,为什么?

export LD_LIBRARY_PATH=~/libis difference from putting LD_LIBRARY_PATH=~/libin ~/.bashrc?

export LD_LIBRARY_PATH=~/lib是把差LD_LIBRARY_PATH=~/lib~/.bashrc

采纳答案by Tim

Without the export your declared LD_LIBRARY_PATH is only valid in the script (.bashrc). With the export it should work, but it is usually not a good idea to set your LD_LIBRARY_PATH like this.

如果没有导出,您声明的 LD_LIBRARY_PATH 仅在脚本 (.bashrc) 中有效。使用导出它应该可以工作,但是像这样设置 LD_LIBRARY_PATH 通常不是一个好主意。

If you don't want to install your library in the system path (e.g. /usr/lib) you should probably use a script that sets LD_LIBARAY_PATH locally and starts your application.

如果您不想在系统路径(例如 /usr/lib)中安装您的库,您可能应该使用一个在本地设置 LD_LIBARAY_PATH 并启动您的应用程序的脚本。

回答by Aaron Digulla

Try $HOME/libinstead of ~/lib- it shouldbe the same but I've seen cases where ~wasn't expanded properly when used in an variable assignment.

尝试$HOME/lib而不是~/lib- 它应该是相同的,但我见过~在变量赋值中使用时没有正确扩展的情况。

To check, try echo $LD_LIBRARY_PATHwhich gives you the current value.

要检查,请尝试echo $LD_LIBRARY_PATH哪个为您提供当前值。

Re export: If you omit the export, then the variable is only known to the current shell process and will not be exported to child processes. So if you omit it, echo $LD_LIBRARY_PATHwill get the value because the variable is expanded by the shell beforethe echocommand/builtin has a chance to do anything. But ./testwon't see it because it's not exported to the new subprocess.

Re export: 如果省略export,则该变量仅对当前shell 进程已知,不会导出到子进程。因此,如果省略它,echo $LD_LIBRARY_PATH将获得该值,因为该变量在命令/内置命令有机会执行任何操作之前echo已被 shell 扩展。但./test不会看到它,因为它没有导出到新的子流程。