c/c++与lua互相调用

c/c++与lua互相调用


如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033


环境:
lua:5.3.5(2018-06-26)
mingw:7.3.0 64bit
gcc:4.9.3

1.lua简介

Lua是一种强大,高效,轻量级,可嵌入的脚本语言。 它支持过程编程,面向对象的编程,功能编程,数据驱动的编程和数据描述。

Lua将简单的过程语法与基于关联数组和可扩展语义的强大数据描述结构结合在一起。 Lua是动态类型的,可通过基于寄存器的虚拟机解释字节码来运行,并具有带有增量垃圾回收的自动内存管理功能,因此非常适合配置,脚本编写和快速原型制作。

2.lua安装

2.1 windows

set path=D:\Qt\Qt5.12.9\Tools\mingw730_64\bin;%path%
set include=D:\Qt\Qt5.12.9\Tools\mingw730_64\include;%include%
set lib=D:\Qt\Qt5.12.9\Tools\mingw730_64\lib;%lib%

cd lua-5.3.5\src
mingw32-make mingw

可以得到liblua.a和lua53.dll

2.2 linux

sudo apt-get install libreadline-dev
curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make linux

默认生成的是liblua.a静态库,如果需要动态库需求修改Makefile

3.c/c++调用lua

// hello.lua

print "hello lua"
//main.cpp

#include "lua.hpp"

int main()
{
    lua_State *lua_state = luaL_newstate();

    static const luaL_Reg lualibs[] =
    {
        { "base", luaopen_base},
        { NULL, NULL}
    };

    const luaL_Reg *lib = lualibs;
    for(; lib->func != NULL; lib++)
    {
        luaL_requiref(lua_state, lib->name, lib->func, 1);
        lua_pop(lua_state, 1);
    }

    luaL_dofile(lua_state, "hello.lua");

    lua_close(lua_state);
}
$ tree
.
├── hello.lua
├── lua
│   ├── include
│   │   ├── lapi.h
│   │   ├── lauxlib.h
│   │   ├── lcode.h
│   │   ├── lctype.h
│   │   ├── ldebug.h
│   │   ├── ldo.h
│   │   ├── lfunc.h
│   │   ├── lgc.h
│   │   ├── llex.h
│   │   ├── llimits.h
│   │   ├── lmem.h
│   │   ├── lobject.h
│   │   ├── lopcodes.h
│   │   ├── lparser.h
│   │   ├── lprefix.h
│   │   ├── lstate.h
│   │   ├── lstring.h
│   │   ├── ltable.h
│   │   ├── ltm.h
│   │   ├── luaconf.h
│   │   ├── lua.h
│   │   ├── lua.hpp
│   │   ├── lualib.h
│   │   ├── lundump.h
│   │   ├── lvm.h
│   │   └── lzio.h
│   └── lib
│       └── liblua.a
└── main.cpp
$ g++ -o main main.cpp -I./lua/include -L./lua/lib -llua
$ ./main
hello lua

4.lua调用c/c++

// hello.lua

print "hello lua"

hellocpp()
// main.cpp
#include "stdio.h"
#include "lua.hpp"

static int hellocpp(lua_State* luaEnv)
{
    printf("hello cpp");
    return 0;
}

int main()
{
    lua_State *lua_state = luaL_newstate();

    static const luaL_Reg lualibs[] =
    {
        { "base", luaopen_base},
        { NULL, NULL}
    };
    const luaL_Reg *lib = lualibs;
    for(; lib->func != NULL; lib++)
    {
        luaL_requiref(lua_state, lib->name, lib->func, 1);
        lua_pop(lua_state, 1);
    }

    luaL_openlibs(lua_state);
    lua_register(lua_state, "hellocpp", hellocpp);

    luaL_dofile(lua_state, "hello.lua");

    lua_close(lua_state);
}
$ g++ -o main main.cpp -I./lua/include -L./lua/lib -llua
$ ./main
hello lua
hello cpp

License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎

如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033


Reference:
NULL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itas109

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值