Lua’s a simple, light-weight scripting language that supposedly is pretty easy to hook into C++. And that’s true… provided you meet a few conditions, such as static member methods if using lua_register() inside a class. While I won’t go into too many details at the moment concerning that (since I’m still researching them), I can say that it works pretty darn well for linking methods outside of classes (which are inheritantly pseudo-static anyway).
I will give it this much: Lua is extremely easy to build, especially in a GCC or Visual C++ environment. To install it in Visual C++, simply create a new project and import every *.h and *.c file in the source directory into the project except print.c and one other (which elludes me, but the compile process will flag it). In VC, you’ll need to compile the project as a DLL and as a static LIB file. I then copied these into C:\Program Files\lua\lib and linked it into Visual C++ Express Edition. Then copy every header file into a new includes directory in the same lua location, add it to VC EE, and done.
The Lua package is intended for a C environment, but C++ support is easily done by including the extern‘ed wrapper, “lua.hpp”. From there, to get Lua up and running in C++, there are only a few needed calls:
#include <stdio.h>
#include <lua.hpp>
int myFunction( lua_State *state )
{
printf("\tLUA passed argument: %s", lua_tostring(state,-1) );
lua_pushnumber(state,0);
return 1;
}
int main()
{
//open the Lua virtual machine
luaState *vm = lua_open();
//register the c++ function
lua_register( vm, "l_function", myFunction );
//call our Lua script (can be pre-compiled)
luaL_dofile(vm, "./script");
//shut down the lua virtual machine
lua_close(vm);
return 0;
}
To get this to compile in G++, simply pass it the “-llua” argument (NOTE: I haven’t needed -llualib or -lauxlib thus far in my Lua travels).
Next, we write up our Lua script to be used within this C++ app:
l_function( "argh" );
Compile this Lua script if you want with “luac -o script_compiled script_name“. Compile the C++ class, execute it, and you’re done.
Next up: registering a non-static function in Lua. At the moment, I’m playing with tolua++. More to follow.