无法编译 LibOTR

Cannot compile LibOTR

本文关键字:LibOTR 编译      更新时间:2023-10-16

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

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