4

According to the Lua Wiki, if Lua 5.1 or later is compiled as C++, it will use C++ exceptions. Since Lua is a C library it only references CC in the makefile. So, my idea was to redefine CC as "g++" so I don't have to modify the makefile.

make generic CC="g++"

I was able to build Lua without any problems. However, now when I link my C++ application to the Lua (static) library I receive undefined reference errors for many Lua functions (lua_checklstring, lua_pushinteger, etc).

When I build Lua using using gcc my application links successfully.

make generic CC="gcc"

Am I compiling Lua for C++ incorrectly? I assume I need to use g++ somehow since the Lua source code contains preprocessor checking for cplusplus to determine if C++ exceptions should be enabled.

pqvst
  • 4,344
  • 6
  • 33
  • 42
  • Which lua header are you including in your C++ project (lua.h or lua.hpp)? And are you using extern "C" with it? – Etan Reisner Dec 30 '13 at 15:49
  • I am using lua.hpp. I am not using extern "C" around lua.hpp since lua.hpp already does that. Should I be including lua.h (without extern "C") since my Lua library is now a C++ library (rather than a C library)? – pqvst Dec 30 '13 at 20:26
  • 1
    Correct. `extern "C"` avoids the C++ name mangling which you need done since your library was built as C++. – Etan Reisner Dec 30 '13 at 20:37
  • It sounds like the compiled lua runtime is getting its api function names mangled. Can you check your compiled static library with `objdump` or `nm` and verify that the names aren't mangled? – greatwolf Dec 31 '13 at 01:25
  • Also see http://lua-users.org/wiki/BuildingLua – dualed Dec 31 '13 at 13:53

1 Answers1

3

As mentioned in my comment to the original question, I was including lua.hpp in my C++ application. This worked well when Lua was compiled as a C library since lua.hpp undos name mangling:

lua.hpp:

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

As pointed out by @EtanReisner, when using Lua as a C++ library instead I do not need to undo the name mangling. So the solution was to simply include the actual Lua headers, rather than lua.hpp

// Using Lua as a C library
#include <lua.hpp>

// Using Lua as a C++ library
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
pqvst
  • 4,344
  • 6
  • 33
  • 42