luabind:不能调用基本的lua函数,如print,tostring。
luabind: Can't call basic lua functions like print, tostring
我想这是一个非常基本的问题:
调用lua的C++代码如下所示:
lua_State* m_L;
m_L = lua_open();
luabind::open(m_L);
luaL_dofile(m_L, "test.lua");
try {
luabind::call_function<void>(m_L, "main");
} catch (luabind::error& e) {
std::string error = lua_tostring(e.state(), -1);
std::cout << error << std::endl;
}
lua_close(m_L);
现在test.lua有以下内容:
function main()
print "1"
end
执行后,我收到错误:
test.lua:2: attempt to call global 'print' (a nil value)
问题出在哪里?这和环境有关吗?我认为像print这样的函数是在全局环境中定义的。为什么没有找到?
非常感谢。
正如您所了解的,您必须调用luaopen_base
才能获得print
和其他基本函数。然后你需要调用luaopen_string
,luaopen_math
来获取基本的模块和函数。不用手动写出来,可以用luaL_openlibs
:一次加载所有的Lua基本函数
lua_State* m_L = luaL_newstate();
luaL_openlibs(m_L);
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么当我调用 print() 函数时我的字符串不会输出?
- 如何解决,在使用元编程的条件基类的情况下没有成员函数"print"
- 在为 QScriptEngine 重新定义"print()"函数时返回"Undefined Value"有什么意义?
- "print 1 to n"函数打印 1 两次
- luabind:不能调用基本的lua函数,如print,tostring。
- 从另一个函数初始化print()中的成员
- 如果添加print语句,相同的Rcpp函数返回不同的输出
- 如何在没有像print(f)这样的标准C函数的情况下将char转换为八进制格式
- 链表实现-使用单独的Insert(), Delete(), Print()函数
- 从print函数打印2d数组的问题
- 一个Debug-Print函数来控制它们
- 类 'TestSub' 中未声明'void TestSub::print()'成员函数