How to use Lua Extended CC ++ Application Series 1

xiaoxiao2021-03-05  24

Author: buxiu Transfer: Lua Chinese website (www.luachina.net)

Lua is a lightweight scripting language that can be easily used to extend C / C applications. Here we learn how LUA extends C / C through several simple examples. First, you need to install Lua, if you use the Windows operating system you can download someone to compile the binary, of course, you can also download Lua Source Codes (http://www.lua.org/download.html) Use your favorite compiler to compile into binary files. If you use Linux, you only need to download the source code to decompress the file file and execute Make, then execute make install as root users .unix operating system, you download source code After modifying the configuration file in Install, you can perform similar operations under Linux. Of course, if you want to use Lua in any path, you don't forget to modify the .profile file Add your environment variable; Windows The next environment variable needs to be set. The first program: This program is very short, but it can explain the problem, first explain the few points: 1. Call Lua_Open () will create a pointer to the LUA interpreter. 2. LUA_BASELIBOPEN () Function LUA library. 3. Use Lua_Dofile () to load scripts and run the script. 4. Lua_Close () is turned off the pointer to the Lua pointing interpreter. Save the following code as Luatest.cpp, if you like to use C instead of C , you need to save the file as Luatest.c and remove extern.

Code:

#include extern "C" {#include "Lua.h" #include "lualib.h" #include "lauxlib.h"} / * the Lua Interpreter * / Lua_State * L; int Main (int Argc) , char * argv []) {/ * Initialize Lua * / L = Lua_Open (); / * LUA BASE LIBRARIES * / Lua_Baselibopen (L); / * Run the script * / Lua_dofile (L, "Test.lua") ; / * Cleanup Lua * / Lua_Close (L); return 0;}

Below is a simple lua script: - Simple Test

Code:

Print "Hello, World!"

Confirm that it can run. Compile: Use your favorite compiler to compile the C / C files above, under Linux as an example: Command Board Type:

Code:

G LuateSt.cpp-la -llib -o luatest

If there is no error, run the program:

Code:

./luatestest

转载请注明原文地址:https://www.9cbs.com/read-32743.html

New Post(0)