gpt4 book ai didi

包含子包的 Lua 包

转载 作者:行者123 更新时间:2023-12-04 22:12:40 28 4
gpt4 key购买 nike

我用 C 为 Lua 编写了很多模块。每个模块都包含一个 Lua 用户数据类型,我像这样加载和使用它们:

A = require("A")
B = require("B")
a = A.new(3,{1,2,3})
b1 = B.new(1)
b2 = B.new(2) * b1

现在我想将两种用户数据类型放在一个共享库 AandB 中,可以像这样使用

AB = require("AandB")
AB.A.new(3,{1,2,3})

实现这一目标的好方法是什么?现在我的 luaopen_* 函数看起来像这样

int luaopen_A(lua_State *L) {
luaL_newmetatable(L, A_MT);
luaL_setfuncs(L, A_methods, 0);

luaL_newlib(L, A_functions);

return 1;
};

然后是否仍然可以只加载部分,例如像这样:A = require("AandB.A")?

最佳答案

require("AandB.A") 在您的 C 库中定义 luaopen_AandB_A 时有效,它必须被称为 AandB.so

通常,require 在尝试 C 库时用下划线替换点。

关于包含子包的 Lua 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27304108/

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