Tonight, it took two more minutes to toss Lua and C + + interconnect, and finally succeeded, and felt the need to record it. To tell the truth, the search engine is really pros and cons, you have a place to search the answer, the disadvantage is that you can not find the right answer or even find the answer to mislead you, tonight more deepened my experience, but finally toss out some results.
Pre-Preparation: Install Luaforwindows (LFW), install Visual Studio 2013 (In fact, 6.0 is enough).
Next, open vs, create a new solution, add another project under the solution, and as my first example, create a new Win32 console program. Then, right-click the project to set its properties:
Configuration Properties->vc++ directory, executable directory , set LFW directory, example: E:\Program\lua\5.1
Configuration Properties->vc++ directory, including directories , setting include folders, for example: E:\Program\lua\5.1\include
Configuration Properties->vc++ Directory--set lib directory , example: E:\Program\lua\5.1\lib
additional dependencies, inputs, linker , adding "Lua5.1.lib;lua51.lib"
Next, send the C + + code, patchwork on the net, add a mix of your own code:
1 //Test.cpp: Defines the entry point of the console application. 2 //3#include"stdafx.h"4#include"stdlib.h"5 extern "C"{6#include"lua.h"7#include"Lualib.h"8#include"Lauxlib.h"9 };Ten OneLua_state *L; A intLuaadd (intXinty) - { - intsum; theLua_getglobal (L,"Add"); - Lua_pushnumber (L, x); - Lua_pushnumber (L, y); -Lua_call (L,2,1); +sum = (int) Lua_tonumber (L,-1); -Lua_pop (L,1); + returnsum; A } at - int_tmain (intARGC, _tchar*argv[]) - { - intsum =0; -L =Lua_open (); - luaopen_base (L); in lual_openlibs (L); -Lual_loadfile (L,"Add.lua"); toLua_pcall (L,0, Lua_multret,0); +sum = Luaadd (Ten, the); -printf"The sum is%d\n", sum); the Lua_close (L); *System"PAUSE"); $ return 0;Panax Notoginseng}
There are also Add.lua scripts:
-- Simple example, do only integer addition function add (x, y) return x + yend
The script is saved in the project directory, and the CPP file lives together.
Finally see the console display "The sum is 25", although the whole process is written as if it is very simple appearance, but toss it out to know how hard the process of exploration!