我在家里创建了自己的图书馆目录(/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
你有解决方案吗?
感谢。
答案 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
的修改将在应用程序启动时用于查找正确的库(而不是在编译时)。