// helloworld.cpp
#include <iostream>
#include <luabind>
void greet()
{
std::cout << "hello world!\n";
}
// int luaopen_[libname] is the entry point
extern "C" int luaopen_libhelloworld(luaState* L)
{
using namespace luabind;
open(L);
module(L)
[
def("greet", &greet),
];
return 0;
}
The above, when compiled into a .so, works as expected.
require 'libhelloworld'
greet()
-- prints "hello world!"
Unfortunately, chaos sets in when I attempt to bind a simple class, e.g.:
class Test
{
public:
Test();
void say() { std::cout << "Inside a class!" << std::endl; }
};
...
def("greet", &greet),
class_<Test>("TestClass")
.def(constructor<>())
.def("say", &Test::say)
...
at which point the undefined symbols start flying as I try to load the library in Lua. Specifically:
- ./libhelloworld.so: undefined symbol: _ZN4TestC1Ev
The literature on Lua binary modules appears to be archaic and almost every example I've come across is either in pure C or C-like C++ (e.g. the working example above). I realize the error is most likely caused by name mangling but I'm not sure how to resolve it. I investigated exporting symbols with GCC by using __attribute__((__visibility__("default"))), but that doesn't appear to have any effect. Some sparse documentation recommended appending -WI -E to the GCC arg list and that doesn't appear to have done much either (-E is just a preprocessor pass?).
The only other valid example I can find is the Ogre bindings to Lua (using Luabind) which doesn't use an entry point at all as far as I can tell. Unfortunately, I'm not entirely clear how they got away with that since anytime I try Lua - surprise - whines about not having a valid entry point.
I realize this should probably be elsewhere (like on the mailing list) but I was kind of hoping my problem was either obvious or a Luabind guru was somewhere nearby. Any help would be greatly appreciated.
EDIT: SOLVED. I forgot to define the constructor. *hangs head in shame* My apologies folks, this is precisely why sleep is important.