c++ - 嵌入式 Lua C++ : how can i load multiple lua modules from C++ side

标签 c++ module lua loading

在我的应用程序中,我想在加载 lua 脚本之前在 Lua 中加载一个基础库。

例子:

测试库.lua

A = 5
B = 6

function foo(a,b)
    return a+b
end

测试.lua

c = foo(A,B)

在我的 C++ 模块中,我想做这样的事情

// load the lib 
luaL_loadbuffer(L, libText, libSize, "testLib");
// run it so that the globals are known
lua_pcall(L,0,0,0);
// load the main script that uses the lib function and variables
luaL_loadbuffer(L, progText, progSize, "testLib");
// run it
lua_pcall(L,0,0,0);

这里我得到一个错误,函数'foo'是未知的

有没有办法在同一个 lua 状态下加载多个 Lua 模块?

提前感谢您的帮助

最佳答案

您需要先绑定(bind)函数 foo。

http://lua-users.org/wiki/BindingCodeToLua

在绑定(bind) c 数学函数的示例中展示了如何做到这一点

关于c++ - 嵌入式 Lua C++ : how can i load multiple lua modules from C++ side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9411809/

相关文章:

import - ES2015 `import * as` 与普通 `import` 之间的区别

lua - 如何将标准输出重定向到 Lua 中的文件?

c - 将具有整数值的 double 值转换为整数时,是否保证执行 'properly' ?

lua - 如何解决这个解包问题?

C++0x 错误 : variable 'std::packaged_task<int> pt1' has initializer but incomplete type

c++ - 从 Luabind 调用 C++ 成员函数导致 "No matching overload found"

perl - 如何在不安装 CPAN 模块的情况下安装 deps?

c++ - 生成随机大小写字母背后的逻辑

c++ - 从文件 : constructor taking std:ifstream& as argument 实例化