根据文档,GCC 在 LD_LIBRARY_PATH 中查找路径以链接共享库,但在我的情况下它似乎被忽略了!
echo $LD_LIBRARY_PATH
-->:/home/mehrdad/usr/lib
(因此 LD_LIBRARY_PATH
已正确设置)
我在“/home/mehrdad/usr/lib
”中有 libfoo.so
但是:
gcc main.c -lfoo
返回错误:
/usr/bin/ld: cannot find -lfoo
collect2: error: ld returned 1 exit status
所以有什么问题??? LD_LIBRARY_PATH
是否已弃用???!
但我可以成功链接到显式命令:
gcc main.c -L/home/mehrdad/usr/lib -lfoo
而且我也可以通过以下方式成功执行 a.out:
./a.out
似乎 LD_LIBRARY_PATH 被 OS 库加载器而不是 GCC 所尊重!
我的环境: 操作系统:CentOs 7 编译器:gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
最佳答案
我错了!感谢 Alexandre C
和 David Schwartz
!
LD_LIBRARY_PATH
仅用于加载程序(运行时)。
LIBRARY_PATH
根据文档我需要的是:
The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it cannot find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).
关于c - LD_LIBRARY_PATH 被 GCC 忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47105835/