我无法链接libfortuna库

lal*_*rde 1 c++ linux linker g++ undefined-reference

我在Linux下构建了FreeBSD libfortuna库.我做了什么来管理这只是评论#include <malloc_np.h>的src/px.hSRC/internal.h,做make,make 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;
}
Run Code Online (Sandbox Code Playgroud)

然后编译并链接:

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
Run Code Online (Sandbox Code Playgroud)

我也尝试了-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
Run Code Online (Sandbox Code Playgroud)

我检查了符号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
Run Code Online (Sandbox Code Playgroud)

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

Lig*_*ica 7

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

C++不是C.

你可以尝试:

extern "C" {
   #include <fortuna.h>
}
Run Code Online (Sandbox Code Playgroud)

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