这有什么问题吗?我有以下简单的课程:
#include "libmnl/libmnl.h" int main() { struct mnl_socket *a = mnl_socket_open(12); }
在运行一个简单的gcccompile(gcc -lmnl main.c)之后,出现以下错误:
gcc
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-dev的 和 libmnl0 包安装。的strace输出gcc表明ld正使用该* .so文件:
strace
ld
[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
库必须在使用它们的对象之后列出(更确切地说,仅当库包含满足遇到未定义引用的符号时,才使用库)。将移至-lmnl命令末尾。
-lmnl