编译c源代码时找不到-lagent(不兼容的库)
在 ubuntu 中使用 gcc 我使用这个命令来编译我的源代码:
gcc 1.c -L. -lagent -lm -lpthread -o 1
但我得到了这个错误:
/usr/bin/ld: skipping incompatible ./libagent.so when searching for -lagent
/usr/bin/ld: cannot find -lagent
collect2: ld returned 1 exit status
我该如何解决这个问题?
With gcc in ubuntu I used this command to compile my source code:
gcc 1.c -L. -lagent -lm -lpthread -o 1
but I got this error:
/usr/bin/ld: skipping incompatible ./libagent.so when searching for -lagent
/usr/bin/ld: cannot find -lagent
collect2: ld returned 1 exit status
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
链接器告诉您文件
./libagent.so
存在,但格式不正确。它可能是一个空文件,或者是为 32 位而不是 64 位构建的,也可能是指向错误版本的符号链接。
The linker is telling you that the file
./libagent.so
exists, but isn't in the appropriate format.It could be an empty file, or built for 32-bit instead of 64-bit, or it could be a symlink pointing to the wrong version.
让我们首先看看您的命令行参数。
gcc 1.c -L。 -lagent -lm -lpthread -o 1
您使用
1.c
输入源代码调用编译器gcc
,然后指定一个附加(链接)包含当前目录的库路径 (.
)-L.
。然后,您告诉它链接到代理和 pthread 库,其中共享(动态)库具有 libNAME.so 的默认名称格式,其中 NAME 替换为名称。静态库具有默认文件扩展名.a
(来自术语archive)。然后将输出(在本例中为可执行文件)指定为文件1
(数字一,而不是字母“ell”)。/usr/bin/ld: 搜索 -lagent 时跳过不兼容的 ./libagent.so
这是链接器 (
ld
) 告诉您文件 ./libagent.so (它可能在当前目录中找到)不是预期的有效共享库格式。这可能适用于不同的机器架构(x86-64、ARMle、PowerPC、MIPS)或不兼容的库格式(我不知道库文件 .so 是否有任何COFF
或ELF
或PE
依赖项与否)。或者只是空的或损坏的(例如,由于编译/链接错误而导致输出中断)。因此,您通常希望不将当前目录包含在链接器的搜索路径中,除非您拥有尚未安装的库的副本(通常安装到 /usr/ lib/ 或 /usr/local/lib/),例如您编写了该库并希望在安装之前将测试程序链接到它。
面向 Debian 和 Unbuntu 的部分答案:
通常您需要安装共享库的运行时组件(通常命名为
libagent
)和相关的开发文件(最常见的是至少一个头文件,希望是一个联机帮助页) ),格式为libagent-dev
。基于 RPM 的 Linux 系统使用 libagent-devel 风格的命名约定(从内存中)。因此,如果这是包的名称,则 sudo aptitude install libagent-dev 应该可以解决问题。Let's look at your command line parameters first.
gcc 1.c -L. -lagent -lm -lpthread -o 1
You call the compiler
gcc
with the input source code of1.c
and then you specify an additional (link) library path to include the current directory (.
)-L.
. Then you tell it to link against the agent and pthread libraries, where shared (dynamic) libraries have the default name format of libNAME.so where NAME is replaced with the name. Static libraries have the default file extension.a
(from the term archive). Then you specify the output (executable in this case) to be the file1
(digit one, not the letter 'ell')./usr/bin/ld: skipping incompatible ./libagent.so when searching for -lagent
This is the linker (
ld
) telling you that the file ./libagent.so (it found presumably in the current directory) is not a valid shared library format as it was expecting. This could be for a different machine architecture (x86-64, ARMle, PowerPC, MIPS) or a incompatible library format (I don't know if library files, .so, have anyCOFF
orELF
orPE
dependencies or not). Or simply otherwise empty or corrupted (e.g. interrupted output due to errors compiling / linking).So you normally want to not include your current directory in your linker's search path, unless you have the copy of the library that you have not yet installed (typically to /usr/lib/ or /usr/local/lib/), such as you wrote the library and wish to link test programs to it before you install it.
Debian and Unbuntu-oriented part of the answer:
Normally you want to install shared library's runtime component (often named something like
libagent
) and the associated development files (most often at least a header file and hopefully a manpage) in the formatlibagent-dev
. RPM based Linux systems uselibagent-devel
style naming conventions (from memory). Sosudo aptitude install libagent-dev
should do the trick if that is the package's name.