求救,在QT5.13.0中调用Lua5.3.0
lua吧
全部回复
仅看楼主
level 1
777✨777 楼主
系统环境:ubuntu18.04
下载编译好了lua5.3.0,并且在QT中添加了lua的库
当执行luaL_openlibs(L)时, 程序编译出错了
当把luaL_openlibs(L)屏蔽掉时,可以继续运行程序
helloworld.lua文件格式如下:
str = "hllo"
table1 = {name = "cz", id = "996"}
function add(a,b)
return a+b
end
QT程序如下:
//1.创建Lua状态
lua_State *L = luaL_newstate();
if(L == nullptr) return ;
//luaL_openlibs(L); //载入lua基础库
//2.加载并运行lua文件
int bRet = luaL_loadfile(L, "helloworld.lua");
if(!bRet){
qDebug() <<"load file error" << endl;
return ;
}
lua_pcall(L,0,0,0);
//4.读取变量
lua_getglobal(L,"str");
if(lua_isstring(L,1)){
QString str = lua_tostring(L,1);
qDebug() << "str:" << str << endl;
}else {
qDebug() << "read str errorrrrrrrr" << endl;
return ;
}
//5.读取table
bRet = lua_getglobal(L,"table1");
if(!bRet){
qDebug() <<"read table1 error" << endl;
return ;
}
lua_getfield(L,2,"name");
QString name = lua_tostring(L,2);
qDebug() << "table1.name: " << name << endl;
//6.读取函数
bRet = lua_getglobal(L,"add"); // 获取函数,压入栈中
if(!bRet){
qDebug() <<"read function add error" << endl;
return ;
}
lua_pushnumber(L,10); // 压入第一个参数
lua_pushnumber(L,20); // 压入第二个参数
// 调用函数,调用完成以后,会将返回值压入栈中,2表示参数个数,1表示返回结果个数。
bRet = lua_pcall(L,2,1,0);
if(!bRet){
qDebug() <<"work function add error" << endl;
return ;
}
if(lua_isnumber(L,-1)){
double valus = lua_tonumber(L,-1);
qDebug() << "10+20:" << QString::number(valus) << endl;
}
//7.关闭state
lua_close(L);
return ;
结果为什么是:
不能输出str,table和函数等信息
求求大佬指点迷津,救救孩子!!!!!!!
2020年11月18日 06点11分 1
level 8
你把string变量当成函数调用了
2020年11月26日 05点11分 2
1