How to embed Lua 5.1 in C++

Lua, is a scripting language providing dynamic data structures, maths, io and string manipulations just like any interprete language such as Bash, Python, Ruby etc.

What is so special about Lua? 
Lua is Fast, Light-weight and Embeddable.

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


Before you start, please download and install Lua from HERE.
Disclaimer: My example are 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 user
print ("Hi " .. name .. ", enjoy hacking with Lua");

2. Write a cpp program clua.cpp to execute foo.lua.

01extern "C" {
02#include "lua.h"
03#include "lualib.h"
04#include "lauxlib.h"
05}
06 
07int main()
08{
09    int s=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    return 0;
24}

Lua API are in c format, in order to make it work with C++ you need to extern “C”. luaL_openlibs(L) loading up all the basic libs such as IO, String, Math etc. I believe if you are using Lua 5.0, you have to replace luaL_open(L) to load the libs one by one like this:

      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 you program with libdl besides liblua. I believe the use of luaL_openlibs() are calling dlopen, dlclose, dlerror, dlsym which needs libdl.

Else you may 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

$ ./clua
Please enter your name: surface
Hi surface, enjoy hacking with Lua

I 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 will be in RED, check out 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 script 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 will share more when I learn more tricks!

Reference and Tutorial!
Lua provides excellent documentation:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值