How to use Lua Extended CC ++ Application Series 1- Transfer from BBS.LUAChina.Net

xiaoxiao2021-03-05  29

Getting Started with Lua

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 a good binary, of course, you can also download Lua source code (

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 use the root user Execute make install. UNIX operating system, you download the source code, perform similar operations under Linux by modifying the configuration file in installs in INSTALL.

Of course, if you want to use Lua in any path, you don't forget to modify the .profile file Add your environment variable; you need to set the environment variable under Windows.

The first program:

This program is very short, but can explain the problem, first explain a 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:

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;

}

Here is a simple lua script:

SIMPLE TEST

Code

RINT "Hello, World!"

Confirm that it can run.

Compile:

Use your favorite compiler to compile the C / C files above above, following linux as an example:

Command line Type:

Code: g Luatest.cpp - Llua -llualib -o Luatest

If there is no error, run the program:

Code: ./ luatest

The program should be printed: Hello, World!

If you are not a Linux operating system, use the VC compiler, you need:

1. Create a new Win32 console application project.

2. Add file luatest.cpp to your project.

3. Go to Project, settings Click the Link page.

4. Add Lua lib.lib to the Object / Library Modules list.

5. Press F7 compiler.

Before running the program, you need to make sure that the Lua Lib.dll file is placed in Windows, copy this file from C: / Program Files / Lua-5.0 to the Visual C Project directory, if there is no error, now Ctrl F5 runs the program. Through this example we have embedded in the first step in embedding Lua in C / C , the next section describes how to call the Lua function in C / C and pass the return value of the function to the C / C program.

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

New Post(0)