问题描述
这里可能有什么问题?我有以下简单的类:
#includelibmnl / libmnl.h
int main (){
struct mnl_socket * a = mnl_socket_open(12);
}
运行简单的 gcc
compile( gcc -lmnl main.c
)我得到以下错误:
/tmp/cch3GjuS.o:在函数`main'中:
main.c :(。text + 0xe):未定义的引用'mnl_socket_open'
collect2:ld返回1退出状态
在共享库上运行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 包。 gcc
的 strace
输出表明 ld
正在使用* .so文件:
pre $ [pid 10988] stat(/ usr / lib / gcc / x86_64-linux-gnu / 4.6 / libmnl.so,0x7fff2a39b470)= -1 ENOENT(没有这样的文件或目录)
[pid 10988] open(/ usr / lib / gcc / x86_64-linux-gnu / 4.6 / libmnl.so (/ usr / lib / gcc / x86_64-linux-gnu / 4.6 / libmnl.a,0x7fff2a39b4d0)= -1 ENOENT(没有这样的文件或目录)
[pid 10988] open(/ usr / lib / gcc / x86_64-linux-gnu / 4.6 / libmnl.a,O_RDONLY)= -1 ENOENT (/ usr / lib / gcc / x86_64-linux-gnu / 4.6 /../../../ x86_64-linux-gnu / libmnl.so,0x7fff2a39b470) = -1 ENOENT(没有这样的文件或目录)
[pid 10988] open(/ usr / lib / gcc / x86_64-linux-gnu / 4.6 /../../../ x86_64-linux- gnu / libmnl.so,O_RDONLY)= -1 ENOENT(没有这样的文件或目录)
[pid 10988] stat(/ usr / lib / gcc / x86_64-linux-gnu / 4.6 /../。 ./../x86_64-linux-gn (/ usr / lib / gcc / x86_64-linux-gnu / 4.6 /../。)。 ./../x86_64-linux-gnu/libmnl.a,O_RDONLY)= -1 ENOENT(没有这样的文件或目录)
[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
$ c $在使用它们的对象之后必须列出库(更确切地说,库仅用于使用它们)。
如果它包含满足在遇到它时已知的未定义参考的符号)。将 -lmnl
移到该命令的末尾。
What could be wrong here? I have the following simple class:
#include "libmnl/libmnl.h"
int main() {
struct mnl_socket *a = mnl_socket_open(12);
}
And after running a simple gcc
compile (gcc -lmnl main.c
) I get the following errors:
/tmp/cch3GjuS.o: In function `main':
main.c:(.text+0xe): undefined reference to `mnl_socket_open'
collect2: ld returned 1 exit status
Running nm on the shared library shows that it's actually found:
aatteka@aatteka-Dell1:/tmp$ nm -D /usr/lib/libmnl.so | grep mnl_socket_open
0000000000001810 T mnl_socket_open
This is happening on Ubuntu 12.04. The libmnl-dev and libmnl0 packages are installed. The strace
output of gcc
indicates that ld
is using exactly that *.so file:
[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
Libraries must be listed after the objects that use them (more precisely, a library will be used only if it contains a symbol that satisfies an undefined reference known at the time it is encountered). Move the -lmnl
to the end of the command.
这篇关于即使nm指示该符号存在于共享库中,也是未定义的符号引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!