Lua learning: Build a lua Environment

Source: Internet
Author: User

In addition to an extended speech, Lua is also a "glue language ". The most famous is game Script Development.

Here we mainly talk about how Lua interacts with C.

The main method for communication between Lua and C is an ubiquitous virtual stack. Almost all API calls will operate on the value on this stack. All data exchanges, from Lua to C or C to Lua, are completed through this stack. Stack can solve the two major differences between Lua and C. The first difference is that Lua uses garbage collection, while C requires explicit memory release. The second is that Lua uses dynamic types, the C language uses the static type.


After downloading the Lua source code, you can choose to compile it on different platforms. Compilation is also very simple. The following is the compilation method in windows. (I am using lua5.1. I feel that the new version has changed a lot and there are no more than 5.1 tutorials .)

1. Open vs's command line tool --> Visual Studio 2005 Command Prompt
2. Jump to the upper-level directory of etc, for example, CD/D: \ Lua
2. Execute: etc \ luavs. BAT (Note: \ is not/, cannot execute compilation if it is wrong)
3. Then lua51.dll, lua51.lib, lua.exe, and luac.exe are generated in the SRC path.
Of course, you can also customize the name of the generated file. For details, see luavs. bat.

Then you can write the first program that interacts with Lua.

# Include <stdio. h> # include <string. h> extern "C" {# include "lua. h "# include" lauxlib. h "# include" lualib. h "} # pragma comment (lib," lua. lib ") int main (void) {char buff [256]; int error; lua_State * L = luaL_newstate (); // create a new environment luaL_openlibs (L ); // open the standard library while (fgets (buff, sizeof (buff), stdin )! = NULL) {error = luaL_loadbuffer (L, buff, strlen (buff), "line") | lua_pcall (L, 0, 0); if (error) {fprintf (stderr, "% s", lua_tostring (L,-1); lua_pop (L, 1 ); // error message popped up from Stack} lua_close (L); return 0 ;}

Header file lua. h defines the basic functions provided by lua, including creating the lua environment, calling the lua function (such as lua_pcall), reading and writing global variables in the lua environment, and registering new functions for lua to call. All content defined in Lua. h has a lua _ prefix.

The header file lauxlib. h defines the functions provided by the auxiliary library (auxiliary library, auxlib. All its definitions start with luaL _ (such as luaL_loadbuffer ). The auxiliary library is a high-level abstraction layer compiled using APIs in lua. h. All the standard libraries of Lua use the auxiliary libraries.

Note: The compilation and execution of Lua scripts are independent of each other and executed on different threads. You can use the luaL_newstate () function to apply for a virtual machine and return the pointer type lua_State. In the future, all other Lua Api functions require this pointer as the first parameter to specify a virtual machine. Therefore, lua_State represents a lua virtual machine image, and luaL_newstate () allocates a virtual machine. The lua Class Library manages all virtual machines. Destroys all the images of a specified Virtual Machine (this method will be called if there is no method related to garbage collection) and reclaim all the memory dynamically allocated by the virtual machine, in some platforms, we do not need to call this function, because when the main program exits, the resources will be released naturally, but a long-running program, for example, for a web server running in the background, the virtual machine resources need to be recycled immediately to avoid high memory usage.

The interfaces from lua5.0 to lua5.2 are slightly different, and the Environment is slightly different. For details, see README in the source code.

Additional reading:

Lua 5.1 references: http://www.codingnow.com/2000/download/lua_manual.html

Lua users wiki: http://lua-users.org/wiki/


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.