C++和Lua的互调

#include <iostream>
#include <string>
using namespace std;

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

//C++调用Lua
int CPPUseLua() {
    lua_State *L = luaL_newstate();

    //加载lua文件
    int bRet = luaL_loadfile(L, "test1.lua");
    if (bRet) {
        cout << bRet << "loadfile fail!" << endl;
        return 0;
    }

    //运行lua文件
    bRet = lua_pcall(L, 0, 0, 0);
    if (bRet) {
        cout << bRet << "pcall fail!" << endl;
        return 0;
    }

    //获取lua中的变量
    lua_getglobal(L, "num"); //将num压入栈
    int num = lua_tonumber(L, -1); //获取栈顶元素
    cout << num << endl;

    lua_getglobal(L, "str"); //将str压入栈
    string str = lua_tostring(L, -1); //获取栈顶元素
    cout << str << endl;
    //读取lua的表
    lua_getglobal(L, "tbl"); //将tbl压入栈
    lua_getfield(L, -1, "name"); //将栈顶table中的name字段压栈
    string name = lua_tostring(L, -1); //获取栈顶元素
    cout << name << endl;

    //调用函数
    lua_getglobal(L, "add");
    lua_pushnumber(L, 15); //压入参数
    lua_pushnumber(L, 25); //压入参数

    bRet = lua_pcall(L, 2, 1, 0); //调用栈顶往下的第二个函数,参数个数为2,返回值个数为1

    int ret = lua_tonumber(L, -1); //读取返回值
    cout << ret << endl;


    //关闭L
    lua_close(L);
}

static int average(lua_State *L) {
    /* 得到参数个数 */
    int n = lua_gettop(L);
    double sum = 0;
    int i;

    /* 循环求参数之和 */
    for (i = 1; i <= n; i++)
    {
        /* 求和 */
        sum += lua_tonumber(L, i);
    }
    /* 压入平均值 */
    lua_pushnumber(L, sum / n);
    /* 压入和 */
    lua_pushnumber(L, sum);
    /* 返回返回值的个数 */
    return 2;
}


//Lua调用C++
int LuaUseCPP() {

    lua_State *L = luaL_newstate();
    //注册函数
    lua_register(L, "average", average);
    //加载文件
    int bRet = luaL_loadfile(L, "test2.lua");
    if (bRet) {
        cout << bRet << "loadfile fail!" << endl;
        return 0;
    }
    //运行lua文件
    bRet = lua_pcall(L, 0, 0, 0);
    if (bRet) {
        cout << bRet << "pcall fail!" << endl;
        return 0;
    }
    cout << lua_gettop(L) << endl;

    //测试是否返回正确结果
    lua_getglobal(L, "avg");
    lua_getglobal(L, "sum");
    cout << lua_gettop(L) << endl;

    cout << lua_tonumber(L, -1) << endl;
    lua_remove(L, -1);
    cout << lua_tonumber(L, -1) << endl;
    return 0;
}


int main() {

    LuaUseCPP();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++Lua交互可以通过Lua提供的C API实现。Lua提供了一组C函数,可以在C++用这些函数来实现与Lua的交互。下面是一个简单的示例: 假设我们有一个Lua脚本文件test.lua,内容如下: ``` -- test.lua function add(a, b) return a + b end ``` 现在我们想在C++用这个add函数,可以按照以下步骤实现: 1. 在C++代码中引入Lua的头文件 ```c++ extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } ``` 2. 创建一个Lua虚拟机 ```c++ lua_State* L = luaL_newstate(); ``` 3. 加载Lua脚本文件 ```c++ int ret = luaL_dofile(L, "test.lua"); if (ret != LUA_OK) { const char* err = lua_tostring(L, -1); printf("Failed to load script: %s\n", err); lua_close(L); return 1; } ``` 4. Lua函数 ```c++ lua_getglobal(L, "add"); lua_pushnumber(L, 1); lua_pushnumber(L, 2); int ret = lua_pcall(L, 2, 1, 0); if (ret != LUA_OK) { const char* err = lua_tostring(L, -1); printf("Failed to call function: %s\n", err); lua_close(L); return 1; } int result = lua_tonumber(L, -1); printf("Result: %d\n", result); ``` 上面的代码中,我们首先lua_getglobal函数获取Lua全局变量add,然后使用lua_pushnumber将两个参数1和2压入栈中,接着lua_pcall函数用函数,并将返回值从栈中弹出,最后使用lua_tonumber获取函数返回值。 需要注意的是,Lua的栈是一个先进后出的数据结构,我们使用lua_push系列函数将数据压入栈中,使用lua_to系列函数获取栈中的数据,使用lua_pop函数将数据从栈中弹出。 总的来说,C++Lua交互可以通过Lua提供的C API实现,需要使用一些基本的函数来操作Lua的栈和Lua函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

achonor

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

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

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

打赏作者

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

抵扣说明:

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

余额充值