千家信息网

lua脚本中怎么调用so文件

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,本篇文章为大家展示了lua脚本中怎么调用so文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。#include #include #include
千家信息网最后更新 2025年02月03日lua脚本中怎么调用so文件

本篇文章为大家展示了lua脚本中怎么调用so文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

#include #include #include
#include
#include

// lua 脚本5.1.5 int getA(lua_State* luaEnv)
{
char a[44] = "test"; // 压入栈 lua_pushstring(luaEnv,a);
return 1;
}

// 方法列表.
static luaL_Reg luaLibs[] =
{
{"getA", getA},
{NULL, NULL}
};

// 调入方法入口必须有luaopen_ 开启 int luaopen_show(lua_State* luaEnv)
{

const char* const LIBRARY_NAME = "show";  // 注册

luaL_register(luaEnv, LIBRARY_NAME, luaLibs);

 return 1;

}

打包c 命令 gcc -c -fPIC -o show.o show.c gcc -shared -o show.so show.o

lua 脚本调用 show.so local show= require "show" a=show.getA() 打印 a 为 test

上述内容就是lua脚本中怎么调用so文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0