How to embed Lua 5.1 in C + +

Source: Internet
Author: User
Tags lua

http://blog.csdn.net/cnjet/article/details/5909555

Lua, is a scripting language providing dynamic Data structures, maths, IO and string manipulations just as any interpret E language such as Bash, Python, Ruby etc.

What's so special about Lua?
Lua is Fast, Light-weight and embeddable.

Lua can is embedded into C and C + + programs and Lua core is statically complied with your C + + programs, meaning your C + + Program itself is now becoming LUA interpreter that execute LUA scripts. To extend your C + + application to execute Lua script was simple, lets check it out.


Before you start, please download and install Lua from here.
Disclaimer:my example is based on Lua 5.1.3.

1. Create a simple Lua script and name it as Foo.lua.

io.write("Please enter your name: ")name = io.read() -- read input from userprint ("Hi " .. name .. ", enjoy hacking with Lua");

2. Write a CPP program Clua.cpp to execute Foo.lua.

01 extern"C"{
02 #include "lua.h"
03 #include "lualib.h"
04 #include "lauxlib.h"
05 }
06
07 intmain()
08 {
09     ints=0;
10
11     lua_State *L = lua_open();
12
13     // load the libs
14     luaL_openlibs(L);
15
16     //run a Lua scrip here
17     luaL_dofile(L,"foo.lua");
18
19     printf("/nI am done with Lua in C++./n");
20
21     lua_close(L);
22
23     return0;
24 }

The Lua API is in c format, and in the order to do it work with C + + need to extern "C". Lual_openlibs (L) loading up all the basic libs such as IO, String, Math etc. I believe if you is using Lua 5.0, you have to replace Lual_open (L) to load the libs one by one

      luaopen_base(L);      luaopen_table(L);      luaopen_io(L);      luaopen_string(L);      luaopen_math(L);

3. Compile the clua.cpp with g++.

g++ -o clua{,.cpp} -llua -ldl

important! You need to link your program with LIBDL besides Liblua. I believe the use of lual_openlibs () is calling Dlopen, Dlclose, Dlerror, dlsym which needs LIBDL.

Else you could get linking error like this:

/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc‘:loadlib.c:(.text+0x917): undefined reference to `dlsym‘loadlib.c:(.text+0x924): undefined reference to `dlerror‘loadlib.c:(.text+0x9fc): undefined reference to `dlopen‘loadlib.c:(.text+0xa11): undefined reference to `dlerror‘/usr/local/lib/liblua.a(loadlib.o): In function `gctm‘:loadlib.c:(.text+0x101e): undefined reference to `dlclose‘collect2: ld returned 1 exit status

4. Execute your C + + app

$ ./cluaPlease enter your name: surfaceHi surface, enjoy hacking with LuaI am done with Lua in C++.

Cool isn ' t it?

Lets try to change Foo.lua into this:

io.write("Please enter your name: ")name = io.read()io.write("Hi " .. string.format("/27/91/1;38;40m%s/27/91/0;47;40m",name) .. ", enjoy hacking with Lua/n");

Now, run ./clua again! You name'll is in RED, check out the text color example here

Ofcause, in order to really extend your C + + apps to Lua script, you need more than Lua_dofile (), you need to allow Lua SCR IPT calling your C + + functions, you may need to access variables from Lua to C + + and vice versa. Well, mean while I am still learning, I'll share more if I learn more tricks!

Reference and tutorial!
Lua provides excellent documentation:

    • 1. Lua Reference Manual
    • 2. Ebook:programming in Lua

I am dilemma here whether should I post this at http://linux.byexamples.com orhttp://cc.byexamples.com. As an introduction post for Lua programming, I'll put it on both blogs. For the post, if I write about Lua scripting, I'll post at http://linux.byexamples.com and if I write about Lua C + + A PI, it'll be at http://cc.byexamples.com.

Hope you enjoy this post, and start to script with Lua.
@lightstar: In the case of you is reading this, I would like to say thank-introduce this wonderful language to me, I Enj Oy it very much!

How to embed Lua 5.1 in C + +

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.