Always know that cocos2dx Lua is exported through the tolua++ LUA interface, but has not been self-directed, recently relatively idle, tried the next.
My environment is: Ubuntu, after installing the tolua++, you can try the tolua++ tool to export under the command line.
MyClass.cpp file:
#include <iostream> #include "tolua++.h" class Myclass{public:void Say () {std::cout << "Hello Wor ld! "<< Std::endl; }};
tolua++ need to write pkg file, similar to C + + 's. h file, then remove the keyword, myclass.pkg
#include "MyClass.h" class myclass{MyClass (); ~myclass (); void say ();};
In terminal execution:
tolua++-o MyClass.cpp myclass.pkg//-o MyClass.cpp means writing to the MyClass.cpp file.
After execution, the MyClass.cpp file will be generated, open will find a good hundreds of lines of code, there will be a
Tolua_api int Luaopen_myclass (lua_state* tolua_s)
, and then include a reference to the MyClass.h file in the file :
#include "string.h" #include "tolua++.h" ... #include "MyClass.h" ... Tolua_api int Luaopen_myclass (lua_state* tolua_s)//This method is the method to be exported to Lua
Add the function declaration in the MyClass.h file:
/* Exported function */tolua_api int tolua_myclass_open (lua_state* tolua_s);
MyTest. Lua Test files:
Local my = MyClass () My:say ()
Execute LUA through the main.cpp file.
#include <iostream> #ifdef __cplusplus extern "C" { #endif #include "Tolua++.h" #ifdef __cplusplus} #endifextern "C" {#include "lua.h" #include "Lualib.h" #include "Lauxlib.h"} #include "MyClass.h" tolua_api int tolua_myclass_open (lua_state* tolua_s); Int main () { lua_state* l = lua_open (); lual_openlibs (l); tolua_myclass_open (l); lual_ Dofile (l, "Mytest.lua"); // execute Lua script file lua_close (L); return 0;}
In Terminal input:
g++ main.cpp myclass.cpp-llua-ldl-ltolua++-o Test_lua
-LXX is the compilation rule for g++, which represents the library file that needs to be linked.
The result will print: Hello World
tolua++ Trial Demo