The test environment is vs200520.luaforwindows_v5.1.4-45.exe + WIN7.
1.install luadevelopment environment luaforwindows_v5.1.4-45.exe
2. Start VS2005 and select "Win2 console application" in the "win32" project type under C ++"
3. tool -- option -- project and solution -- VC ++ directory -- executable Program (C: \ Program Files \ Lua \ 5.1); contains file (C: \ Program Files \ Lua \ 5.1 \ include); library Files (C: \ Program Files \ Lua \ 5.1 \ lib ); project -- 'Project name' property sub-menu -- configuration Property -- linker -- input -- Additional dependency (lua51.lib lua5.1.lib)
For detailed configuration of part 1, refer to this document in Baidu Library:
Build Lua programming environment on
Www.2cto.com
4. Then, program in the "Project name. cpp" file as follows:
// CYLua. cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
// This is a C ++ program, so it must be extern "C ",
// Because the lua header files are in the C format
Extern "C "{
# Include "lua. h"
# Include "lualib. h"
# Include "lauxlib. h"
}
/* The Lua interpreter */
Lua_State * L;
Int luaadd (int x, int y)
{
Int sum;
/* The function name */
Lua_getglobal (L, "add ");
/* The first argument */
Lua_pushnumber (L, x );
/* The second argument */
Lua_pushnumber (L, y );
/* Call the function with 2
Arguments, return 1 result */
// Indicates that the called function has two parameters and one return value.
Lua_call (L, 2, 1 );
/* Get the result */
Sum = (int) lua_tonumber (L,-1 );
Lua_pop (L, 1 );
Return sum;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int sum;
/* Initialize Lua */
L = lua_open ();
Luaopen_base (L );
/* Load Lua base libraries */
LuaL_openlibs (L );
/* Load the script */
/* Lua01.lua is the Lua file called by the C ++ program. Note that the second parameter of the luaL_dofile () function must be an absolute path, otherwise, the system prompts "unprotected error in call to lua api (attampt to call a nil value)" error */
LuaL_dofile (L, "D :\\ LuaTestDoc \ Lua01.lua ");
/* Call the add function */
Sum = luaadd (10, 15 );
/* Print the result */
Printf ("The sum is % d", sum );
/* Cleanup Lua */
Lua_close (L );
Return 0;
}
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Open the Lua integrated development environment of SciTE and edit the Lua01.lua file as follows (this program places this file in the D: \ LuaTestDoc \ path, when the Lua file is loaded and compiled, write: luaL_dofile (L, "D: \ LuaTestDoc \ Lua01.lua ");):
Function add (x, y)
Return x + y
End
Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Finally, press ctrl + F5 to compile and run The code. The running result will be: The sum is 25.
Author: angxiao