我构建了 FreeBSD libfortuna library在 Linux 下。我为管理它所做的只是评论 #include <malloc_np.h>
在 src/px.h 和 src/internal.h 中,执行 make
, make install
,并在我的系统标准路径中创建库的符号链接(symbolic link)。
然后,我为它写了一个小测试程序:
#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
我不知道该怎么办了。欢迎提供一些线索。
最佳答案
查看 Fortuna 源码,它是一个 C 库,没有 C++ 的兼容代码;在我看来,这会导致您在调用约定和名称修改方面出现问题。
C++ 不是 C。
你可以试试:
extern "C" {
#include <fortuna.h>
}
通过 C 链接强制将库的声明带入您的代码,但这有点 hack。理想情况下,库的 header 将在内部使用此技术以按照约定启用 C++ 兼容性。
关于c++ - 我无法链接到 libfortuna 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313718/