Introduction to mutual invocation of LUA and C + +

Source: Internet
Author: User

Enter the game industry, start learning the advantage of Lua,lua scripting language is flexible, the game industry application is quite extensive, because support dynamic loading, so it is essential to exist. You can think, if you stick to C + + does not put, there is a bug, do not fix the bug after restarting the server? How much time does it have to waste?

All right, crap, talk less, on the flow.

1: Download Lua Source: http://www.lua.org/ftp/

2: Set up the console application, select "Static Library", remove "Precompiled header" option

3: Add the downloaded LUA source to the project, as

4: Select release mode

5: To prevent 4996 errors, add "_crt_secure_no_warnings" to the "preprocessor"

6: OK, compile build.

The source code compiles the work to end here, enters the second step

Two:

1: New console application, including source directory, in order to facilitate the future application, the Lib file renamed "Lua.lib", source files into a single folder;

2: In the project Properties "Additional Include directory" contains the LUA source directory, bloggers here on the C drive.

3: "Additional Dependencies" contains Lib, remember to put in the current working directory, do not forget. (Remind students of self-taught programming)

Three: Start the last step, heap code.

The C + + code is as follows:

#include"stdafx.h"#include<iostream>extern "C"{#include<lua.h>#include<lualib.h>#include<lauxlib.h>}lua_state*L;Static intFrist_lua (Lua_state *L) {     intNum =lua_tonumber (L,1); Std::cout<<num<<Std::endl; Lua_pushstring (L,"Hello Lua");return 1;}int_tmain (intARGC, _tchar*argv[]) {L= Lual_newstate ();//registering a state machineLual_openlibs (L);//Loading LUA LibrariesLua_register (L,"Frist_lua", Frist_lua); Lual_dofile (L,"C:\\hellolua.lua");//Loading LUA ScriptsLua_close (L);//freeing a virtual machineGetChar (); return 0;}

Lua Code:

local hello = Frist_lua (1)print(hello)

Operation Result:

Finally, there are a number of third-party plug-ins for real application Lua to interact with C + +, which are recommended here:

Lua Tinker ( lua5.0 developed plug-ins, is said to be Korean, with the update 5.1 can also be used, 5.2 can not be used by the online authors do not know where to go, fortunately, the vast number of program monkeys themselves clothed, 5.2 changes can be used under, such as the next, including Lib and library can be,/HTTP download.csdn.net/download/kingsollyu/6888211) Bloggers Use this, more good.

tolua++
Use the MIT License. : http://www.codenix.com/~tolua///This reputation is also good ~! Tastes ~

Introduction to mutual invocation of LUA and C + +

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.