[英]Calling a c function from lua 5.2 generates a syntax error
我一直在尝试让lua脚本在我正在开发的一款小型游戏上工作,但是lua似乎比它的价值还要麻烦。 经过大量的谷歌搜索和头发撕裂之后,我设法运行了简单的脚本,但是很快就碰壁了。 C函数似乎不想绑定到lua,或者至少不想在绑定后运行。 g ++编译C代码时没有发生任何事件,但是lua解释器生成了以下语法错误:
LUA ERROR: bin/lua/main.lua:1: syntax error near 'getVersion'
我的C(++)代码:
#include <lua.hpp>
static const luaL_Reg lualibs[] =
{
{"base", luaopen_base},
{"io", luaopen_io},
{NULL, NULL}
};
void initLua(lua_State* state);
int getVersion(lua_State* state);
int main(int argc, char* argv[])
{
lua_State* state = luaL_newstate();
initLua(state);
lua_register(state, "getVersion", getVersion);
int status = luaL_loadfile(state, "bin/lua/main.lua");
if(status == LUA_OK){
lua_pcall(state, 0, LUA_MULTRET, 0);
}else{
fprintf(stderr, "LUA ERROR: %s\n", lua_tostring(state, -1));
lua_close(state);
return -1;
}
lua_close(state);
return 0;
}
void initLua(lua_State* state)
{
const luaL_Reg* lib = lualibs;
for (; lib->func != NULL; lib ++)
{
luaL_requiref(state, lib->name, lib->func, 1);
lua_settop(state, 0);
};
delete lib;
}
int getVersion(lua_State* state)
{
lua_pushnumber(state, 1);
return 1;
};
Lua代码:
print getVersion()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.