无法将库目录添加到ld

时间:2014-10-27 22:46:53

标签: linux ld

我在家里创建了自己的图书馆目录(/home/mathieu/lib)。我在这个目录中有一个名为libmath.so的库。 要将此目录添加到ld,我已在te文件/etc/ld.so.conf中添加了路径。我通过执行sudo ldconfig重新加载了缓存。 当我执行sudo ldconfig -p | grep libmath.so时,我得到libmath.so (libc6) => /home/mathieu/lib/libmath.so。所以它有效。

问题

当我执行ld -lmath时,我得到:

attempt to open /usr/i486-linux-gnu/lib32/libmath.so failed
attempt to open /usr/i486-linux-gnu/lib32/libmath.a failed
attempt to open /usr/local/lib32/libmath.so failed
attempt to open /usr/local/lib32/libmath.a failed
attempt to open /lib32/libmath.so failed
attempt to open /lib32/libmath.a failed
attempt to open /usr/lib32/libmath.so failed
attempt to open /usr/lib32/libmath.a failed
attempt to open /usr/local/lib/i386-linux-gnu/libmath.so failed
attempt to open /usr/local/lib/i386-linux-gnu/libmath.a failed
attempt to open /usr/local/lib/libmath.so failed
attempt to open /usr/local/lib/libmath.a failed
attempt to open /lib/i386-linux-gnu/libmath.so failed
attempt to open /lib/i386-linux-gnu/libmath.a failed
attempt to open /lib/libmath.so failed
attempt to open /lib/libmath.a failed
attempt to open /usr/lib/i386-linux-gnu/libmath.so failed
attempt to open /usr/lib/i386-linux-gnu/libmath.a failed
attempt to open /usr/lib/libmath.so failed
attempt to open /usr/lib/libmath.a failed
ld: cannot find -lmath

ld甚至无法在/home/mathieu/lib路径中寻找。

gcc test.c -o test -lmath返回

/usr/bin/ld: cannot find -lmath
collect2: error: ld returned 1 exit status

你有解决方案吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用LIBRARY_PATH添加gcc要搜索的目录进行编译,

export LIBRARY_PATH=$HOME/lib

或者,您可以将库添加到-L命令行标记为gcc,如

gcc test.c -o test -lmath -L$HOME/lib

问题是您对/etc/ld.so.conf的修改将在应用程序启动时用于查找正确的库(而不是在编译时)。