【发布时间】:2014-01-07 01:55:13
【问题描述】:
我目前正在学习如何使 Lua 与 C++ 一起工作,我偶然发现了这个问题。 这是我目前正在使用的小应用程序:
#include <lua.5.2.3\src\lua.hpp>
#include <iostream>
#include <string>
int pluacall(lua_State *L){
std::cout << "Called from inside Lua." << std::endl;
return 0;
}
int main(){
std::string x;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_Reg _funcs[] = { { "CppFunc", pluacall }, {} };
if (luaL_dofile(L, "test.lua")){
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
luaL_setfuncs(L, _funcs, 0);
lua_getglobal(L, "sup");
if (lua_pcall(L, 0, 0, 0)){
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
std::cout << "Test";
std::getline(std::cin, x);
lua_close(L);
return 0;
}
在我添加 luaL_setfuncs(...) 之前,一切都运行良好。
现在应用程序崩溃并出现以下错误:
PANIC: Unprotected error in call to Lua API (attempt to index a nil value)
老实说,我完全不知道为什么它不起作用或这个错误甚至意味着什么(谷歌今天不是我的朋友)。有什么想法吗?
可能还值得一提的是,我没有将 Lua 库与我的应用程序链接,我将它们编译在一起(所有 Lua 源都添加到项目中)
编辑:这是我正在测试的 Lua 脚本
function sup()
io.write("Hey.\n")
CppFunc()
return 1
end
【问题讨论】:
标签: c++ visual-c++ lua visual-studio-2013