Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Problems with luabind (and how I resovled them)

Problems with luabind (and how I resovled them)

yckx
yckx
Yckx's GameDev Journal · · 2 min read
1,216 0
After trying out several lua binding libraries, I settled on luabind. And then... luabind wasn't fully working. It compiled fine, and my test app compiled fine, but bugs came up when running it. Here's the code:

using namespace luabind;
using namespace std;

class Item
{
public:

string name;

Item( string n ) { name = n; }

string getString() { return name; }

void setString( string n ) { name = n; }
};

int main()
{
lua_State* L = lua_open();

luaL_openlibs( L );
luabind::open(L);

module(L)
[
class_( "Item" )
.def( constructor() )
.def( "getString", &Item::getString )
.def( "setString", &Item::setString )
.def_readwrite( "name1", &Item::name )
.property( "name2", &Item::getString, &Item::setString )
];

const char *script =
"a = Item('testing')\n"
"a:setString('Hello World')\n"
"print( a:getString() )\n"
"print( a.name1 )\n"
"print( a.name2 )\n";

if( luaL_dostring( L, script ) )
cout << lua_tostring( L, -1 ) << endl;

lua_close( L );

getchar();
return 0;
}

I was getting results like this:


Hello World
function: 0020C680
function: 0020C810

Clearly, def_readwrite and property were not working as documented.

Using Google, I found several posts on several fora from people with the same problem, and no solutions. I joined the luabind-users mailing list and asked there, and received some friendly help, but ended up finding a solution independently of the list. Well, I can't really say that. I don't know that I'd have kept looking if Nigel from the list wasn't so friendly and willing to help out. So, thanks, Nigel!

I considered that I might have had a bad build, even though I didn't notice any errors. And luabind uses boost. I don't know which specific boost libraries it uses--many are just headers--but some must be built, and the readout during building is quite long, so I couldn't easily tell if there were errors or not. After some more searching, I found an article on CodeProject detailing how to build boost on MSVC. Both boost and luabind use bjam to build, and both have instructions basically saying to navigate to the library's root directory and simply run bjam. That's what I had done the first time around.

Following the CodeProject article, I specified some options to bjam:

bjam toolset=msvc-9.0 variant=debug threading=multi link=static define=_BIND_TO_CURRENT_VCLIBS_VERSION
The variant can be debug or release, and link can be static or shared. The important things here seem to be the define and specifying the MSVC version. bjam documentation indicates it can autodetect the proper version, but I figure it doesn't hurt to tell it, and if you have multiple versions installed, you probably ought to be sure it's choosing the correct one.

So, I rebuilt boost and luabind using the above options, both debug and release. I thin rebuilt my test app, and running it I got


Hello World
Hello World
Hello World

... and there was much rejoicing. I hope this post may help others having the same problem.

Discussion

Loading comments...