c++ - 我无法链接到 libfortuna 库

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

我构建了 FreeBSD libfortuna library在 Linux 下。我为管理它所做的只是评论 #include <malloc_np.h>src/px.hsrc/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/

相关文章:

linux - 函数局部的静态变量

ruby - 我怎样才能完全重置我的 Ruby 安装?

c++ - 拥有同名库的静态和动态版本是一种常见的做法吗?

c - 使用静态库编译的共享库中 undefined symbol

C++ 异常; int 还是 std::exception?

c++ - 程序在启动后立即以退出状态 0 退出

c++ - 转储对象的内存内容

linux - rpm 命令在 centOS 7 上不起作用?

c++ - 错误 : 'class' does not name a type C++

c - 强制某些编译器生成的变量进入特定的 ELF 部分(使用 gcc)