tolua++ Trial Demo

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.