C ++ calls the Lua programming environment to build and test the code example

Source: Internet
Author: User

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

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.