From: Joseph T. <to...@gm...> - 2012-01-10 19:21:33
|
Hello, I am trying to use SWIG to generate Lua bindings for a minimal test case C++ program, so I can learn to use it with larger projects. I want to learn how to call a Lua script from C/C++ and in turn I want that Lua script to be able to call C/C++ functions. In my project I have created two files, test.h and test.cpp, and I am compiling Lua 5.1 as if it were C++ (I wanted to reduce extern statements while I learn) and statically linking against it. I have a simple header with some basic swig commands in it. I know that normally I would want to generate a header myself for the SWIG output, or use a number of "extern" statements to declare the functions I wanted to access, but for this simple test I wanted access to everything (until I figure out what I need) so I included the swig output as "luabinding.h". I can currently call Lua core functions, like print, but I cannot load my module with require or call PrintHello. I think the module should already be loaded because it is all statically linked, and I was of the understanding that something in Lua or SWIG tracked that. I have also stepped through the code enough to know that PrintHello is being passed to Lua by the SWIG output as a CClosure, but I am not sure how correct it is. Any advice would be appreciated. Here are the files and commands I used: I ran swig on my Ubuntu machine as follows ---------- $swig2.0 -c++ -v -lua -importall -o luabinding.h test.h ---------- test.h ---------- #ifdef SWIG %{ #include "test.h" %} %include stl.i %module testscripting #endif void PrintHello(); int PrintErrorMessageOrNothing(int ErrorCode); ---------- test.cpp ---------- #include "test.h" #include "lua.h" // Lua Core #include "lualib.h" // for opening the base state #include "lauxlib.h" // Extra Lua Goodies like lua_open() #include "luabinding.h" #include <iostream> int main (int argc, char** argv) { std::cout << "This program will attempt to call a lua script that in turn runs a c function. This was output via cout streaming in C++." << std::endl; lua_State *State; // create a pointer to Track the Lua State State = luaL_newstate(); // Create a Lua State luaopen_testscripting(State); luaL_openlibs(State); int Error = luaL_dostring(State,"print \"Hello from Lua\""); // run a very simple Lua script. PrintErrorMessageOrNothing(Error); //Error = luaL_dostring(State,"require \"testscripting\""); // does work when unremarked Error = luaL_dostring(State,"PrintHello"); // doesnt work either PrintErrorMessageOrNothing(Error); Error = luaL_dostring(State,"print \"Hello from Lua\""); PrintErrorMessageOrNothing(Error); lua_close(State); // Close the Lua state return 0; } int PrintErrorMessageOrNothing(int ErrorCode) { switch(ErrorCode) { case 0: // Fine break; case LUA_YIELD: std::cout << std::endl << "Lua Error Code(LUA_YIELD): " << ErrorCode << std::endl; break; case LUA_ERRRUN: std::cout << std::endl << "Lua Error Code(LUA_ERRRUN): " << ErrorCode << std::endl; break; case LUA_ERRSYNTAX: std::cout << std::endl << "Lua Error Code(LUA_ERRSYNTAX): " << ErrorCode << std::endl; break; case LUA_ERRERR: std::cout << std::endl << "Lua Error Code(LUA_ERRERR): " << ErrorCode << std::endl; break; default: std::cout << std::endl << "Lua Error Code(Unknown Error): " << ErrorCode << std::endl; } return ErrorCode; } void PrintHello() { std::cout << "Hello world! From PrintHello()" << std::endl; } ---------- Output From my program Currently ---------- This program will attempt to call a lua script that in turn runs a c function. This was output via cout streaming in C++. Hello from Lua Lua Error Code(LUA_YIELD): 1 Hello from Lua --------- -- - Joe Toppi (402) 714-7539 To...@gm... |