c++ - 无法编译 LibOTR

标签 c++ c symbols name-mangling

我正在尝试使用 libotr 但在尝试编译一个非常基本的库初始化时遇到以下问题。

#include <libotr/proto.h>

int main(int argc, char const *argv[])
{
  OTRL_INIT;
  // OtrlUserState userstate = otrl_userstate_create();

  return 0;
}

我正在使用以下命令编译它:

g++ main.cpp -o main -L /usr/local/lib/ -lotr

但出于某种原因我得到:

Undefined symbols for architecture x86_64:
  "otrl_init(unsigned int, unsigned int, unsigned int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [bin/bin] Error 1

我明确检查过,图书馆确实有以下符号。

最佳答案

快速观察后,我注意到 libotr 正在使用 C 类型名称重整,只需将以下几行添加到库的 include 子句中即可解决问题:

extern "C" {
  #include <libotr/proto.h>
}

如果您有类似的问题,只需使用 nm 实用程序列出库的符号,并检查符号名称是否以一个或两个下划线开头:_fooC 风格,而 __fooC++ 风格。

附言我发布了这个,因为我花了一段时间才弄明白。我希望这个问题 + 答案能为您节省一些时间。

关于c++ - 无法编译 LibOTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265785/

相关文章:

c++递归循环遍历注册表很慢

c - c11 中的多线程支持

c - 警告 c4996 有关详细信息,请参阅联机帮助

symbols - 如何大声读出 "<<"和 ">>"符号?

c++ - 尝试获取命令行参数的核心转储。 (C++)

c++ - 为什么 if 语句中未定义的参数不会导致段错误或其他错误?

lisp - 如何使用字符串引用属性列表属性

svg - SVG 符号中的线性渐变

c++ - 如何创建一个类,使 vector 起作用std::vector <MyClass <int >> v {1,2,3};

c - 在 C 中的函数中是否必须使用 "return"和 "void"?