未定义的参照符号,即使纳米指示该符号存在于共享库(undefined reference to sy

2019-06-24 04:18发布

可能是什么错在这里? 我有以下简单的类:

#include  "libmnl/libmnl.h"

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

并运行一个简单的后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

在共享库运行纳米表明,它实际上找到:

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文件:

[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

Answer 1:

库必须上市后使用它​​们的对象(更确切地说,一个库将只有当它包含满足它遇到的时间称为一个未定义的参考符号使用)。 移动-lmnl到命令的末尾。



文章来源: undefined reference to symbol even when nm indicates that this symbol is present in the shared library