gpt4 book ai didi

c - C端嵌入Lua模块

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:25 25 4
gpt4 key购买 nike

我想为用户提供几个自定义函数来使用,但隐藏它的实现以将其作为一个简单的 DLL 发布。
所以用户只需链接 dll,插件就可以正常工作。在 C 端动态声明了几个表、整数等,但是声明“类”、C 中的面向表的函数会使代码不可读。
例如,有没有办法将 Lua 模块嵌入到 header 中,并预加载它,以便使用 luaL_loadfile 加载的主脚本用户能够访问它?当然,我可以通过将代码插入用户的脚本来对其进行脏破解,但这似乎是个坏主意。

最佳答案

Is there a way to embed Lua module in a header, for example, and preload it so the main script user loads with luaL_loadfile will be able to access it?

是的。您可以使用 bin2c.lua (或底部列出的任何替代方案)。

这是一个来自名为 fakeredis.c 的项目的真实示例:

Makefile 包含一个专用目标,它创建一个由要嵌入的各种模块的 Lua 字节码组成的 header :

fklua.h:
@echo "converting lua -> C..."
@./lua2c.sh

(lua2c.sh 在后台使用 bin2c.lua)

例如,项目包含一个名为 fmtreply.lua 的模块.转换后,此模块被简单地定义为字节数组:

/* fklua.h */

/* this is the name of the main function the module exposes */
#define FK_LUA_FMTREPLY "_fmtreply"

/* this is the byte code of the module obtained with `bin2c.lua` */
static const unsigned char fk_lua_fmtreply[]={45, 45, 32, ...};

然后模块通过luaL_loadbuffer显式加载到主库中(有关详细信息,请参阅 herethere)。

关于c - C端嵌入Lua模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29075462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com