Lua Tutorial (i): Embedding Lua scripts in C + + _lua

Source: Internet
Author: User
Tags data structures lua

This series of tutorials mainly describes how to embed Lua scripts in C + + programs, and I intend to introduce them in the following ways:

1. How to embed a LUA script in C + +

2.Lua access to C + + data structures (here you will introduce classes, structs, functions, variables, enumerations, and other data types in Lua how to access)

3.c/c++ access to LUA data, primarily basic data types, functions, and table

LUA bindings inside 4.cocos2d-x (with automatic binding and manual binding)

5.cocos2d-x inside the LUA and C + + calls each other

6.cocos2d-x inside Lua and Java call each other

7.cocos2d-x inside Lua and Objective-c call each other

This series of tutorials does not introduce much of the basic syntax of LUA, learning about LUA's basic syntax, and recommending "Programming in Lua". If there are any flaws, please note, thank you.

In addition, about the update time, a weekly, regular or irregular, hehe.

Mac Project Integration Lua

Preparatory work

First, create a new console application. Open Xcode,new->project->osx->command line Tool, named Lesson01 as shown in the following figure:

Next, we need to download lua5.2.3. After downloading, unzip, then the CD to just unzip the path below, enter make MacOSX, so you can generate the Mac below the Lib file.

Next, copy the folder you just unzipped to your new project, and then set the include path and library path. (Note that there is no need to add the head file to the Xcode, as long as the include path is specified, the compiler will automatically find it when compiling.) )

Because the path to my Lua folder is: xxxx/lesson01/lua-5.2.3, so I set search path and library path to:

Copy Code code as follows:

Search Path
$ (srcroot)/lesson01/lua-5.2.3/src/
Library Path
$ (Project_dir)/lesson01

Here is the directory structure between my project and Lua, and the Liblua.a file is copied from the SRC. (We used make macosx to generate it earlier)

Note: If you add all of the LUA source code directly, the error is compiled. Because there is also a main function inside the LUA.C. This main function is used to generate executable programs. In addition, if you are adding a library of other third parties, you can also refer to this method to add search path and library path

C + + call LUA files

First, open the main.cpp and include the bottom file:

Copy Code code as follows:

#include "lua.hpp"

If the compilation does not error at this time, then you set the search path is correct, if the error, please adjust your search path.

The contents of this lua.hpp are as follows:

Copy Code code as follows:

extern "C" {
#include "Lua.h"
#include "Lualib.h"
#include "Lauxlib.h"
}

This is the code in most LUA tutorials. Then add the following in the main function:

Copy Code code as follows:

/1. Initializing the LUA virtual machine
Lua_state *lua_state;
Lua_state = Lual_newstate ();
2. Set up the LUA standard library to be registered, this library is for your LUA script
Because next we just want to output the Hello World in the Lua script, so just introduce the base library.
static const Lual_reg lualibs[] =
{
{"Base", luaopen_base},
{NULL, NULL}
};
3. Register the LUA standard library and empty the stack
Const Lual_reg *lib = lualibs;
for (; Lib->func!= NULL; lib++)
{
Lual_requiref (Lua_state, Lib->name, Lib->func, 1);
Lua_pop (lua_state, 1);
}
4. Run Hello.lua Script
Lual_dofile (lua_state, "Hello.lua");
5. Turn off the LUA virtual machine
Lua_close (lua_state);

New Lua file

Select New->file->other, then name Hello.lua, and here's what's inside the Hello.lua:

Copy Code code as follows:

print "Hello World"

Compile and run

Compile and run at this time, you may not see the console output "Hello World". Because your LUA script is not copied into the program, we need to set it up again. As shown in the following figure: (Note: subpath to empty, copy only when installing remove tick)

At this point, compile and run, and you will get the following results:

Next, let's talk about how the iOS project integrates Lua.

iOS Project Integration Lua

The iOS project integrates Lua in the same way as the Mac, and, incidentally, Lua is essentially a C program, and the method of integrating the corresponding C library under any platform is applicable.

Here I mainly write some notes:

1. We have just compiled the MACOSX library can only be used for Mac programs, iOS need to compile separately (of course, Windows and Linux also need to compile separately, the compilation method see download Lua-5.2.3/doc/readme.html)

2. iOS can be compiled by adding a static library and then adding all the Lua files to the library. Finally, let the main project depend on the library.

3. Direct use of Lual_dofile (Lua_state, "Hello.lua") is not feasible because the iOS project's resource path is in a sandbox. We must obtain the full path to access this Hello.lua file. Taking Cocos2d-x as an example, we can use the following code to get the full path of the Hello.lua and then pass it on to the LUA virtual machine.

Copy Code code as follows:

std::string scriptpath = fileutils::getinstance ()->fullpathforfilename ("Hello.lua");
int status = Lual_loadfile (Lua_state, Scriptpath.c_str ());

Android Project Integration Lua

Android integration Lua needs to use ANDROID.MK to package Lua into a static library, and then include the LUA module in the project. The concrete practice can refer to cocos2d-x.

Related Article

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.