1

I am embedding Lua in a C++ application and I am getting the following linkage errors:

g++     -o dist/Debug/GNU-Linux-x86/testluaembed build/Debug/GNU-Linux-x86/src/main.o build/Debug/GNU-Linux-x86/src/LuaBinding.o -L../../mainline/tanlib_core/dist/Debug/GNU-Linux-x86 -L../../mainline/tanlib++/dist/Debug/GNU-Linux-x86 -L/usr/lib ../../mainline/tanlib_core/dist/Debug/GNU-Linux-x86/libtanlib_core.so ../../mainline/tanlib++/dist/Debug/GNU-Linux-x86/libtanlibpp.so /usr/lib/liblua5.1.a /usr/lib/libtolua++5.1.a /usr/local/boost_1_45_0/stage/lib/libboost_filesystem.a /usr/local/boost_1_45_0/stage/lib/libboost_system.a 
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `gctm':
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/testluaembed] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

Anyone knows why these errors are occurring, and how to fix them?

oompahloompah
  • 9,087
  • 19
  • 62
  • 90
  • Is that really the full error? What happens if you use `-Wl,-v` to pass the `-v` (verbose) option to the linker? Seems like you've probably got a missing input file or symbol or something. – Cascabel Feb 06 '11 at 16:12
  • @Jefromi: you are right. I should have added the so file instead of the static archive file. It works now – oompahloompah Feb 06 '11 at 19:09

2 Answers2

2

In http://lua-users.org/wiki/BuildingLua

there is a note:

Note on embedding Lua in C++ applications

Please note that Lua is a clean subset of ANSI C, and can be compiled as C or C++. Lua headers do not come with {#ifdef __cplusplus extern "C" {#endif ... Lua header ...#ifdef __cplusplus}#endif } in them so that lua can be compiled as C or C++ simply by changing the name of the files, without having to make any changes to the file contents.

If lua was compiled as a C library, which is typical with pre-packaged binaries, in order to embed Lua in a C++ application (i.e. link C to C++) you will have to place extern "C" around the inclusion of the Lua headers in your C++ application, e.g.,

extern "C" {
#include "lua.h"
}

If you do not do this you may get link errors because of C++ name mangling.

Please do not complain about this on the mailing list. :-) Take the time to search the mailing list as this has been covered many times before.

It could be argued that if you are distributing a pre-packaged binaries of the libraries, then you have compiled the lua core as either C (most likely) or as C++, and if you compiled lua as C, you should modify the lua headers to indicate this. However, using prebuilt libraries for lua isn't recommended by the authors, they recommend directly incorporating the lua source into your application. See BuildingModules for a discussion (the end of the page).

By default if lua 5.1 or later is compiled as C++, it will use C++ exceptions to unwind the stack rather than longjmp/setjmp, though this is configurable (at compile time). See luaconf.h near LUAI_THROW/LUAI_TRY for a discussion of this.

user666412
  • 528
  • 8
  • 18
  • Spot on. Also worth noting that `lauxlib.h` and `lualib.h` need to be included the same way (if you need them). – Rob N Mar 21 '12 at 11:43
1

You can just include lua.hpp in your c++ source

StXh
  • 1,856
  • 1
  • 13
  • 16