Linux下Lua和C++交互

前言

lua(wiki 中文 官方社区:lua-users)是一门开源、简明、可扩展且高效的弱类型解释型脚本语言。

由于其实现遵循C标准,它几乎能在所有的平台(windows、linux、MacOS、Android、iOS、PlayStation、XBox、wii等)上运行。

在Lua中,函数是对语句和表达式进行抽象的主要方法。既可以用来处理一些特殊的工作,也可以用来计算一些值。

Lua 提供了许多的内建函数,你可以很方便的在程序中调用它们,如print()函数可以将传入的参数打印在控制台上。

Lua 函数主要有两种用途:

1.完成指定的任务,这种情况下函数作为调用语句使用;

2.计算并返回值,这种情况下函数作为赋值语句的表达式使用。

下面简单总结, lua源码编译,lua脚本与C++交互。

源码编译lua

lua源码下载地址

这里下载了lua-5.4.6

直接cd 到 lua-5.4.6中,执行 make 直接编译 ,会生成liblua.a

c++测试程序源码

首先将lua源码中的所有.h头文件和生成的liblua.a 复制到源码所在目录

main.cpp 源码

cpp 复制代码
#include <iostream>
#include <vector>
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
lua_State* g_pLuaStateEx = NULL;

int Test(lua_State* L)
{
        int n = lua_gettop(L);
        if( n != 1){
                lua_pushboolean(g_pLuaStateEx, 0);
                return 1;
        }
        std::string str = lua_tostring(L, 1);
        std::cout << "Test:" << str << std::endl;
        return 0;
}

bool InitLuaEnv()
{
        g_pLuaStateEx = luaL_newstate();
        luaL_openlibs(g_pLuaStateEx);
        //注册脚本调用函数
        lua_register(g_pLuaStateEx, "Test", Test);
        return true;
}

bool LoadLuaFile(const std::string &luaFile)
{
        int re = luaL_dofile(g_pLuaStateEx, luaFile.c_str());
        if(re){
                const char* pStr = lua_tostring(g_pLuaStateEx, -1);
                if(pStr){
                        std::cerr << pStr << std::endl;
                }
                return false;
        }
        return true;
}
bool CallLuaString(const std::string &str)
{
        std::string strFun = "ParseString";

        lua_getglobal(g_pLuaStateEx, strFun.c_str());
        lua_pushstring(g_pLuaStateEx, str.c_str());
        lua_call(g_pLuaStateEx, 1, 1);
        lua_pop(g_pLuaStateEx, 1);
        return true;
}

bool CallLuaVec(std::vector<std::string> argvs)
{
        std::string strFun = "ParseVec";

        lua_getglobal(g_pLuaStateEx, strFun.c_str());
        lua_newtable(g_pLuaStateEx);
        for(int i=0; i < argvs.size(); i++){
                lua_pushinteger(g_pLuaStateEx, i+1);
                lua_pushstring(g_pLuaStateEx, argvs[i].c_str());
                lua_settable(g_pLuaStateEx, -3);
        }
        lua_call(g_pLuaStateEx, 1, 1);
        lua_pop(g_pLuaStateEx, 1);
        return true;
}

int main(int argc, char* argv[])
{
        //初始化lua脚本
        if(!InitLuaEnv()){
                std::cerr << "Init lua faild!" << std::endl;
        }
        //加载lua脚本
        if(!LoadLuaFile("test.lua")){
                std::cerr << "Init lua faild!" << std::endl;
        }

        //调用lua脚本函数
        if(!CallLuaString("Man call lua ParseString string: xxxx")){
                std::cerr << "CallLuaString error!" << std::endl;
        }

        std::vector<std::string> vec;
        vec.push_back("man call lua ParseVec arg1: test1");
        vec.push_back("man call lua ParseVec arg1: test2");
        vec.push_back("man call lua ParseVec arg1: test3");
        //调用lua脚本函数
        if(!CallLuaVec(vec)){
                std::cerr << "CallLuaVec error!" << std::endl;
        }
        return 0;
}

test.lua 源码

cpp 复制代码
--传字符串
function ParseString(commands)
        print("ParseString:"..commands);
        Test("lua call man Test!");
end
--传数组
function ParseVec(commands)
        print("ParseVec:");
        for i=1,#(commands) do
                print(commands[i]);
        end
        Test("lua call man Test!"); 
end

编译测试程序源码,执行 g++ main.cpp liblua.a -ldl -lm 生成a.out

运行a.out

相关推荐
fpcc6 分钟前
并行编程实战——CUDA编程的流的优先级
c++·cuda
❀͜͡傀儡师6 分钟前
修改centos服务器启动画面
linux·服务器·centos
勇闯逆流河2 小时前
【C++】C++11(下)
开发语言·c++
倔强的石头1063 小时前
openGauss数据库:从CentOS 7.9部署到实战验证
linux·数据库·centos
梁正雄4 小时前
linux服务-Nginx+Tomcat+Redis之Session 共享 - 容器单机版
linux·nginx·tomcat
linchare7 小时前
linux debian上只装mysql的客户端步骤
linux·mysql·debian
胡萝卜3.08 小时前
掌握C++ map:高效键值对操作指南
开发语言·数据结构·c++·人工智能·map
电子_咸鱼8 小时前
【STL string 全解析:接口详解、测试实战与模拟实现】
开发语言·c++·vscode·python·算法·leetcode
百***75749 小时前
linux上redis升级
linux·运维·redis
顾安r9 小时前
11.22 脚本打包APP 排错指南
linux·服务器·开发语言·前端·flask