How to Use Lua to expand C/C ++ application Series 1-from bbs.luachina.net

Source: Internet
Author: User
Getting started with Lua
Lua is a lightweight scripting language that can be easily used to expand C/C ++ applications. Here are a few simple examples to learn how Lua can expand C/C ++.
First, you need to install Lua, if you use Windows operating system you can download other compiled binary program to use, of course you can also download Lua source code (http://www.lua.org/download.html) use your favorite compiler to compile binary files. if you use Linux, you only need to download the source code, decompress the file, execute make, and then execute make install as the root user. for a UNIX operating system, you can download the source code, modify the compilation options in the configuration file install, and then perform similar operations in Linux.
Of course, if you want to use Lua in any path, do not forget to modify the. profile file on Unix-like platforms to add your environment variables. In Windows, you need to set the environment variables.
The first program:
This program is short and small, but it can explain the problem. The first few points are described:
1. Call lua_open () to create a pointer to the Lua interpreter.
2. The lua_baselibopen () function loads the Lua library.
3. Use lua_dofile () to load the script and run it.
4. lua_close () to close the pointer Lua points to the interpreter.
Save the following code as luatest. cpp. If you prefer to use C instead of C ++, you need to save the file as luatest. C and remove extern.

Code: Code:

#include <stdio.h>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();    /* load Lua base libraries */    lua_baselibopen(L);    /* run the script */    lua_dofile(L, "test.lua");    /* cleanup Lua */    lua_close(L);    return 0;}

The following is a simple Lua script:
-- Simple test

Code RINT "Hello, world! "
Make sure it runs.
Compile:
Compile the C/C ++ file saved above with your favorite compiler. The following uses Linux as an example:
Type the following command line:
Code: G ++ luatest. cpp-llua-llualib-O luatest
If there are no errors, run the program:
Code:./luatest
The program should print: Hello, world!
If you are not a Linux operating system and use a VC ++ compiler, You need:
1. Create a new Win32 console application project.
2. Add the file luatest. cpp to your project.
3. Go to the project and click the link page in settings.
4. Add Lua + Lib. lib to the object/library modules list.
5. Compile the program by F7.
Before running the program, make sure that Lua + Lib. place the DLL file in a location that can be found in windows, copy the file from C:/program files/Lua-5.0 to the Visual C ++ project directory, if there is no compilation error, now you can press Ctrl + F5 to run the program.

In this example, we have set foot on the first step of embedding Lua in C/C ++, next we will introduce how to call the Lua function in C/C ++ and pass the return value of the function to the C/C ++ program.

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.