C语言嵌入Lua解释器的方法

Lua语言是一个轻量的脚本语言,可以用很少的资源运行其解释器

C语言是一个很常用的语言,广泛用于嵌入式等底层场景

这两个语言结合,可以应用于嵌入式等多个场景。比如,一些硬件公司会允许开发者使用Lua语言操作其硬件

Lua的安装

这里所指的Lua的安装,就是从Lua: download中下载Lua的源代码,并将其复制粘贴到你的项目中:

解压文件后,进入src目录:

新建一个项目文件夹,然后将src目录中除了lua.c、luac.c的代码文件都复制到项目文件夹中。

然后新建main.c,写入如下代码:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

#include "lua/src/lua.h"     // Lua数据类型与函数接口
#include "lua/src/lauxlib.h" // Lua与C交互辅助函数接口
#include "lua/src/lualib.h"  // Lua标准库打开接口

int main(void){
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);                         // 打开标准库
    luaL_dostring(L, "print('Hello World')"); // 解析并执行Lua脚本字符串
    lua_close(L);                             // 关闭Lua线程
    return 0;
}

编译的时候要注意把lua的源码也编译进去:

cpp 复制代码
gcc main.c ./lua/src/*.c

最后输出:

cpp 复制代码
Hello World

Lua栈

Lua在运行时,有自己的Lua栈,可以使用luaL_newstate()创建一个新的Lua栈:

cpp 复制代码
lua_State* L = luaL_newstate();

可以使用lua_pushXXX压入指定数据类型的数据、lua_isXXX判断某个索引的值是否为指定数据类型,lua_toXXX获得某个索引的值是否为特定类型:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

#include "lua/src/lua.h"     // Lua数据类型与函数接口
#include "lua/src/lauxlib.h" // Lua与C交互辅助函数接口
#include "lua/src/lualib.h"  // Lua标准库打开接口

int main(void){
    lua_State* L = luaL_newstate();

    printf("Top index: %d\n", lua_gettop(L)); // 返回当前栈顶索引(等于栈中元素个数)
    lua_pushnumber(L, 233);                   // 压数据入栈
    lua_pushstring (L, "Hello World");              // 压数据入栈
    printf("Top index: %d\n", lua_gettop(L));

    printf("Index 2: %s\n", lua_tostring(L, 2));  // 将栈中数据转换成C语言数据
    printf("Index 1: %d\n", lua_tointeger(L, 1)); // 将栈中数据转换成C语言数据
                                                  // 这类转换如果失败则给出默认值0或NULL
    printf("Top index: %d\n", lua_gettop(L));

    lua_pop(L, 1);                            // 从栈中弹出一个值
    printf("Top index: %d\n", lua_gettop(L));
    lua_settop(L, 0);                         // 清空栈
    printf("Top index: %d\n", lua_gettop(L));

    lua_close(L);

}

Lua语言调用C语言函数

在Lua中调用的C语言函数必须是

cpp 复制代码
int (*)(lua_State *)

类型的,比如:

cpp 复制代码
int func(lua_State *L){}

使用之前,需要先调用lua_register()函数注册C函数:

cpp 复制代码
lua_register(L, "cfunc", func);

然后调用lua_dostring()执行lua脚本:

cpp 复制代码
luaL_dostring(L, "cfunc()");

完整代码如下:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

#include "lua/src/lua.h"     // Lua数据类型与函数接口
#include "lua/src/lauxlib.h" // Lua与C交互辅助函数接口

int func(lua_State *L){
    const char *str = lua_tostring(L, 1);
    printf("lua want to print : %s\n", str);
    return 0;
}

int main(void){
    lua_State* L = luaL_newstate();

    lua_register(L, "cfunc", func);

    luaL_dostring(L, "cfunc('Hello World')");

    lua_close(L);

}

C语言调用Lua语言函数

C语言调用Lua函数通过栈实现:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include "lua/src/lua.h"
#include "lua/src/lauxlib.h"
#include "lua/src/lualib.h"

// 下面定义了一个lua函数,传入两个参数并打印,返回22,33
const char *lua_code = "\
	function lua_func(arg1, arg2)\
		print(arg1, arg2)\
		return 22, 33\
	end\
";

int main() {
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    luaL_dostring(L, lua_code);  // 加载自定义的lua函数到全局变量

    printf("Top index: %d\n", lua_gettop(L));
    lua_getglobal(L, "lua_func"); // 从全局变量中获取自定义lua函数压入栈中
    lua_pushinteger(L, 666);      // 向栈中压入参数
    lua_pushinteger(L, 777);      // 向栈中压入参数
    printf("Top index: %d\n", lua_gettop(L));
    lua_call(L, 2, LUA_MULTRET);        // 调用已压入栈中的lua函数,传入2个参数,并将所有返回值压入栈中
                                        // lua_call调用函数本身会清空当前栈,然后再将返回值压入栈
    printf("Top index: %d\n", lua_gettop(L));
    printf("Index 1: %s\n", lua_tostring(L, 1)); // 打印lua函数返回值
    printf("Index 2: %s\n", lua_tostring(L, 2)); // 打印lua函数返回值

    lua_close(L);
    return 0;
}

参考

Lua和C语言交互入门_c调用 lua-CSDN博客

GCC编译步骤、动态库和静态库的创建和使用、ELF库简介及查看方法_gcc编译静态库-CSDN博客

相关推荐
软件黑马王子2 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫2 小时前
go orm GORM
开发语言·后端·golang
李白同学3 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
楼台的春风4 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
黑子哥呢?4 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农4 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿5 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风5 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead6 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
风与沙的较量丶6 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言