无法编译 LibOTR

Cannot compile LibOTR

我正在尝试使用 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++ 风格。

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