我无法链接libfortuna库

时间:2014-04-26 16:34:56

标签: c++ linux linker g++ undefined-reference

我在Linux下构建了FreeBSD libfortuna library。我做的是管理它只是在 src / px.h src / internal.h 中评论#include <malloc_np.h>,然后做makemake install,并在我的系统标准路径中创建库的符号链接。

然后,我为它写了一个小测试程序:

#include <cstdlib>
#include <fortuna.h>

int main () {
  int i = 0;
  fortuna_get_bytes (4, (unsigned char *) &i);
  printf ("test fortuna i = %d \n", i);
  return 0;
}

然后编译并链接:

g++ -I/usr/local/include/fortuna -O0 -g3 -Wall -fmessage-length=0 -c test.cpp
g++ -g -L/usr/local/lib/ -o test test.o -lfortuna
test.o: In function `main':
/home/alain/Documents/Poker/WorkSpace/libfortuna/test/test.cpp:14: undefined reference to `fortuna_get_bytes(unsigned int, unsigned char*)'
collect2: ld returned 1 exit status

我也试过-L/usr/lib/我的simlink,但结果相同。

我也尝试使用 gcc 而不是 g ++ ,但也有相同的结果。

我检查了该库是否在路径中:

# ls -l /usr/lib/libfortuna.so
lrwxrwxrwx 1 root root 28 26 avril 17:27 /usr/lib/libfortuna.so -> /usr/local/lib/libfortuna.so

我检查了符号fortuna_get_bytes是否在二进制文件中定义:

$ objdump -T /usr/local/lib/libfortuna.so  | grep fortuna
/usr/local/lib/libfortuna.so:     file format elf64-x86-64
0000000000005000 g    DF .text  000000000000007f  Base        fortuna_get_bytes
0000000000004f80 g    DF .text  000000000000007d  Base        fortuna_add_entropy
$ nm -g -C /usr/local/lib/libfortuna.so | grep fortuna
0000000000004f80 T fortuna_add_entropy
0000000000005000 T fortuna_get_bytes

我不知道该怎么做。欢迎一些线索。

1 个答案:

答案 0 :(得分:7)

检查Fortuna源代码,它是一个没有C ++兼容性代码的C库;在我看来,这样会导致调用约定和名称错误的问题。

C ++不是C。

你可以尝试:

extern "C" {
   #include <fortuna.h>
}

强制使用C链接将库的声明带入您的代码,但这有点像黑客。理想情况下,库的头文件将在内部使用此技术来按照惯例启用C ++兼容性。