简体   繁体   English

LD_LIBRARY_PATH,Linux中的共享库路径

[英]LD_LIBRARY_PATH, the shared lib path in linux

I wrote a shared object, say libsd.so , and I put libsd.so and its header file sd.h in ~/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 test like 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=. 所以我设置了export LD_LIBRARY_PATH=. , then it works. ,那么它起作用了。 But if I unset LD_LIBRARY_PATH and put LD_LIBRARY_PATH=~/lib in my ~/.bashrc , then source ~/.bashrc , again it doesn't work for ./test , WHY? 但是,如果我unset LD_LIBRARY_PATH并将LD_LIBRARY_PATH=~/lib放入我的~/.bashrc ,那么source ~/.bashrc ,同样对于./test ,为什么?

export LD_LIBRARY_PATH=~/lib is difference from putting LD_LIBRARY_PATH=~/lib in ~/.bashrc ? export LD_LIBRARY_PATH=~/lib与将LD_LIBRARY_PATH=~/lib放入~/.bashrc有何不同?

Without the export your declared LD_LIBRARY_PATH is only valid in the script (.bashrc). 如果没有导出,则声明的LD_LIBRARY_PATH仅在脚本(.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通常不是一个好主意。

If you don't want to install your library in the system path (eg /usr/lib) you should probably use a script that sets LD_LIBARAY_PATH locally and starts your application. 如果您不想在系统路径(例如/ usr / lib)中安装您的库,则可能应该使用一个脚本,该脚本在本地设置LD_LIBARAY_PATH并启动您的应用程序。

Try $HOME/lib instead of ~/lib - it should be 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_PATH which 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. 重新export :如果省略export ,则该变量仅对当前的shell进程是已知的,不会导出到子进程。 So if you omit it, echo $LD_LIBRARY_PATH will get the value because the variable is expanded by the shell before the echo command/builtin has a chance to do anything. 因此,如果您忽略它,则echo $LD_LIBRARY_PATH将获得该值,因为在echo命令/内建函数有机会执行任何操作之前 ,shell会扩展该变量。 But ./test won't see it because it's not exported to the new subprocess. 但是./test不会看到它,因为它没有导出到新的子./test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM