2

I am getting trouble compiling a project, basically linking the objects. The linker complains:

$ g++ -o bin/program obj/linux64/Debug/src/main.o  [... more object files ...] ../../../addons/obj/linux64/Debug/ofxLua/libs/lua/lua/lapi.o [...many other object files and libraries...]
../../../addons/obj/linux64/Debug/ofxLua/libs/luabind/src/scope.o: In function `~lua_pop_stack':
../../../addons/ofxLua/libs/luabind/src/scope.cpp:122: undefined reference to `lua_settop(lua_State*, int)'
[...many more undefined reference errors...]

But if I inspect the object file I find the symbol:

$ nm ../../../addons/obj/linux64/Debug/ofxLua/libs/lua/lua/lapi.o | grep lua_settop
000000000000045c T lua_settop

I checked that g++ arguments are correctly ordered.

What can be the cause of this behavior? The only explanation I can think of is that somehow the headers used from different source files are altered somehow by previous includes or defines. Is there any way I can check the resulting complete symbol so I can be sure that's the case?

chaos.ct
  • 1,001
  • 1
  • 7
  • 18

1 Answers1

3

If you are including the header for code written in C you will need to enclose the #include statement within extern "C" { ... } so that the C++ compiler knows to use C-linkage for those symbols, rather than C++-linkage (which uses name-mangling to allow method overloading).

For example:

extern "C" {
#include <file.h>
}

Really the header file itself should do this, in order to avoid you needing to, and I would consider it an error not do so:

//
// file.h
//
#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}    // extern "C"
#endif
trojanfoe
  • 120,358
  • 21
  • 212
  • 242