问题
I've created a cpp file with the main method implementing calls to lua. When compiling + executing the qt project I receive the following error:
PANIC: unprotected error in call to Lua API (attempt to call a string value)
The problem is that lua cannot find the lua file to be executed (at least I think it is). So I copied the file to all the debug dirs and the main dir but it still didn't work.
Thanks for helping me!
main.cpp
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
lua_State* L;
int main(int argc, char *argv[])
{
/* initialize Lua */
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
/* load the script */
luaL_loadfile(L, "test.lua");
lua_call(L, 0, 0);
/* cleanup Lua */
lua_close(L);
}
and the file test.lua
-- test
print("Hello World")
回答1:
Given that this is a run-time file operation, the function is likely only looking in the current directory.
回答2:
It seems like Qt uses the user's directory as default path.
You can validate this with: (thanks Rich)
QDir dir(".");
qDebug() << dir.absolutePath();
When putting the lua script into this folder everything works like a charm.
The working directory can be set with the following command:
QDir::setCurrent()
And in combination with
QCoreApplication::applicationFilePath()
The path can be set to where the exe resigns in.
回答3:
This is not really related to Qt since you are not using any of the Qt API in your software.
You should check the status returned by the lual_loadfile
method like shown in this example. That should give you some additional clues about what is going wrong.
Just in case, there's a QtLua module that could be of interest.
来源:https://stackoverflow.com/questions/41667309/qt-with-lua-where-to-place-lua-files