【问题标题】:Shared library in /usr/local/lib not found未找到 /usr/local/lib 中的共享库
【发布时间】:2011-05-03 17:48:34
【问题描述】:

我不明白。我通常将第三方软件安装到 /usr/local 中,因此库安装到 /usr/local/lib 并且链接到这些库时从来没有问题。但是现在它突然不再起作用了:

$ gcc -lkaytils -o test test.c
/usr/bin/ld.gold.real: error: cannot find -lkaytils
/usr/bin/ld.gold.real: /tmp/ccXwCkYk.o: in function main:test.c(.text+0x15):
error: undefined reference to 'strCreate'
collect2: ld returned 1 exit status

当我添加参数-L/usr/local/lib 时,它可以工作,但我以前从未使用过它。在/usr/local/include中找到头文件,没有添加-I/usr/local/include

我正在使用 Debian GNU/Linux 6 (Squeeze),它在 /etc/ld.so.conf.d/libc 中有一个 /usr/local/lib 条目。 conf 默认情况下,ldconfig 缓存知道我正在尝试使用的库:

k@vincent:~$ ldconfig -p | grep kaytils
        libkaytils.so.0 (libc6,x86-64) => /usr/local/lib/libkaytils.so.0
        libkaytils.so (libc6,x86-64) => /usr/local/lib/libkaytils.so

那么这里到底发生了什么?我在哪里可以检查 gcc 默认搜索哪些库路径?也许那里有问题。

【问题讨论】:

    标签: c linux gcc debian


    【解决方案1】:

    gcc -print-search-dirs 会告诉你编译器检查的路径。 /usr/local/lib 根本不在其中,因此您的编译时链接器(在这种情况下是来自 binutils 的新黄金 ld)找不到库,而动态链接器(ld-linux.so 读取由ldconfig) 确实如此。大概是您之前完成的构建在其 makefile 中根据需要添加了 -L/usr/local/lib(通常由 ./configure 脚本完成),或者您安装了二进制文件。

    【讨论】:

    • 不,我以前从未使用过 -L/usr/local/lib,并且我在另一个 linux 系统(即 Debian 5 Lenny)上进行了测试:即使在 -print-search- 时,它也可以在没有 -L 的情况下工作dirs 没有列出 /usr/local/lib。那么这是新 Debian 系统上的新行为吗?
    • 还在另一个 Debian 6 机器上尝试过。也有效。所以受此影响的只有我自己的系统。
    • 如果是这样,可能与黄金链接器有关。检查 ld -版本。黄金链接器在包 binutils-gold 中并被描述为实验性的。在最近的版本中,普通链接器以 ld.bfd 的形式提供。
    • 找到了。我的系统安装了 binutils-gold 包。不知道为什么 binutils 搜索 /usr/local/lib 而 binutils-gold 不搜索,但现在我不在乎。卸下包装解决了问题。我接受你的回答,因为你告诉我我正在使用 binutils-gold。
    • 对不起,误导性猜测。很高兴你找到它。
    【解决方案2】:

    这可能是环境变量的问题 - 您设置了包含 /usr/local/include 但不包含 /usr/local/lib 的内容

    来自环境变量的 GCC 地图

           CPATH specifies a list of directories to be searched as if speci‐
           fied with -I, but after any paths given with -I options on the com‐
           mand line.  This environment variable is used regardless of which
           language is being preprocessed.
    

           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 can’t 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).
    

    试试“printenv”看看你设置了什么

    【讨论】:

    • printenv 不显示任何包含“PATH”的内容(PATH 本身除外)或包含 GCC 的任何内容。
    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2012-08-16
    • 2015-11-03
    • 2012-02-19
    相关资源
    最近更新 更多