c - LD_LIBRARY_PATH 被 GCC 忽略

标签 c linux gcc

根据文档,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 CDavid 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/

相关文章:

c++ - GCC 编译器无效值错误

c - 在 C 中,释放一半的内存块,而不释放另一半

c - 链接共享库后符号丢失

c - 理解gnu libc的strcmp函数

c - 通过 shell (cat) 或通过程序 (fread) 读取 proc 文件的区别

python ctypes 国外库初始化和取消初始化

c - 为什么 fflush(stdin) 在 while 循环中执行两次以检查 scanf() 输入?

c++ - 使用 <experimental/filesystem> 和调用函数的链接器错误

c++ - 为什么 printf 将 8 位字符填充为 32 位字符?

c - 是否有必要在关闭 epoll 实例本身之前删除兴趣列表中的所有文件描述符?