The method I have used with good results is to give each class that needs Lua bindings a static class method with the following signature:
static luabind::scope luaBindings();
The definition of this method looks like this:
luabind::scope MyClass::luaBindings() { using namespace luabind; return class_<MyClass>("MyClass") .def(constructor<>()) .def("someMethod", &MyClass::someMethod) ; }
Then, in some other place, I initialize the Lua state like this:
void initializeLua( lua_State* L ) { luabind::open(L); luabind::module(L) [ MyClass::luaBindings(), MyOtherClass::luaBindings() ]; }
This way, all Lua bindings are contained in the static class methods. The Lua state is initialized in a very controlled way, without headaches about the order that classes are exported in.
The only downside I have encountered so far is that the initializeLua function needs to know all the classes being exported. Depending on the number of classes, this will result in a rather large list of included files and potentially long compilation times. If that's an issue for you, you can build the luabind::scope hierarchically like this:
luabind::scope moduleABindings() { return ( MyClass::luaBindings(), MyOtherClass::luaBindings() ); } luabind::scope moduleBBindings() { return ( Foo::luaBindings(), Bar::luaBindings() ); } void initializeLua( lua_State* L ) { luabind::open(L); luabind::module(L) [ moduleABindings(), moduleBBindings() ]; }
By separating those functions into separate compilation units, a small change in one of the exported classes' headers will not require recompiling the whole initializer, just the module that has changed.