c++csymbolsname-mangling

Cannot compile LibOTR


I am trying to use libotr but I have the following problem when attempting to compile a very basic library initialisation.

#include <libotr/proto.h>

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

  return 0;
}

I am compiling it with the following command:

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

But for some reason I am getting:

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

I explicitly checked and the library does indeed has the following symbols.


Solution

  • After a quick observation I noticed that libotr is using the C type name mangling and the problem is resolved just by adding the following lines to the library's include clause:

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

    If you have similar problem just list the symbols of a library with the nm utility and check whether the symbol names begin with one or two underscores: _foo is C style, while __foo is C++ style.

    P.S. I posted this since it took me a while to figure it out. I hope this question + answer would save you some time.