31

这里有什么问题?我有以下简单的课程:

#include  "libmnl/libmnl.h"

int main() {
    struct mnl_socket *a = mnl_socket_open(12);
}

运行一个简单的gcccompile ( gcc -lmnl main.c) 后,我得到以下错误:

/tmp/cch3GjuS.o: In function `main':
main.c:(.text+0xe): undefined reference to `mnl_socket_open'
collect2: ld returned 1 exit status

在共享库上运行 nm 表明它确实找到了:

aatteka@aatteka-Dell1:/tmp$ nm -D /usr/lib/libmnl.so | grep mnl_socket_open
0000000000001810 T mnl_socket_open

这发生在 Ubuntu 12.04 上。libmnl -devlibmnl0软件包已安装。的strace输出gcc表明ld正在使用该 *.so 文件:

[pid 10988] stat("/usr/lib/gcc/x86_64-linux-gnu/4.6/libmnl.so", 0x7fff2a39b470) = -1 ENOENT (No such file or directory)
[pid 10988] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/libmnl.so", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10988] stat("/usr/lib/gcc/x86_64-linux-gnu/4.6/libmnl.a", 0x7fff2a39b4d0) = -1 ENOENT (No such file or directory)
[pid 10988] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/libmnl.a", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10988] stat("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libmnl.so", 0x7fff2a39b470) = -1 ENOENT (No such file or directory)
[pid 10988] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libmnl.so", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10988] stat("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libmnl.a", 0x7fff2a39b4d0) = -1 ENOENT (No such file or directory)
[pid 10988] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libmnl.a", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10988] stat("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libmnl.so", {st_mode=S_IFREG|0644, st_size=18608, ...}) = 0
[pid 10988] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libmnl.so", O_RDONLY) = 7
4

1 回答 1

43

库必须列在使用它们的对象之后(更准确地说,只有当库包含满足遇到时已知的未定义引用的符号时,才会使用库)。将 移动-lmnl到命令的末尾。

于 2012-05-04T21:55:53.497 回答