在Qt Creator中将Lua与C ++链接

时间:2018-05-01 00:43:17

标签: c++ qt lua qt-creator

目前我正在尝试将Lua脚本与主机C ++应用程序链接。对于我的Lua脚本,我所拥有的只有:

io.write(string.format("From Lua."));

我正在使用Qt创建器并尝试调用脚本,如下所示:

lua_State *state = luaL_newstate();
lua_call(state , 0 , 0);

不断给我一个PANIC: unprotected error in call to Lua API (attempt to call a string value)错误。

我尝试在这样的函数中包装脚本:

function main()

io.write(string.format("From Lua.With Progress"));


end

然后使用与之前相同的代码调用它,但添加了:

lua_getglobal(state , "main");
lua_pcall(state , 0 , 0 , 0);

没有错误但是:

lua_call(state , 0 , 0) 

发出PANIC: unprotected error in call to Lua API (attempt to call a nil value)错误。

在遇到更多麻烦之后,我发现luaL_loadfile(state , "EngineRxer.lua")返回值7而不是0,所以我假设脚本首先无法加载。

我还检查了Qt Creator 项目中的运行工作目录 - >构建并运行 - >运行,它们与Lua脚本位于同一目录中。

我已经提到过:

Linking Lua with Qt

Qt with Lua | Where to place lua file(s)

甚至还有一些Qt论坛:

http://www.qtcentre.org/threads/60693-Can-not-use-Lua-in-QtCreator

http://www.qtcentre.org/threads/62772-Lua-into-Qt

此外,提及我的.pro文件的样子可能很重要,所有Lua库和包含已经在那里:

LIBS += \
/usr/local/Cellar/lua/5.3.4_3/lib/liblua.5.3.4.dylib

INCLUDEPATH += \
    /usr/local/Cellar/lua/5.3.4_3/include

更重要的是,经过更多的故障排除后,我决定在Netbeans(NonQt只是CML)上制作一个虚拟测试项目,它运行得很好。即使我尝试编写一个虚拟fstream程序来写入空的.txt文件,它也不知道在哪里找到它。所以我怀疑这是一个Qt Creator问题,但我的所有路径和构建目录都指向CWD。

1 个答案:

答案 0 :(得分:1)

以下代码段适用于我:

luaL_openlibs

您是否记得致电lua_load*并加载您的脚本?

请务必检查lua_pcalllauxlib.h的返回值。他们会在堆栈顶部放置一条错误消息,您可以阅读并显示给用户。

另请参阅luaL_loadfile的文档:

  

此函数返回与lua_load相同的结果,但如果无法打开/读取文件,则会有一个额外的错误代码LUA_ERRFILE。

查看LUA_ERRFILE标题(对于lua 5.3),我们发现LUA_ERRERR+1等于lua.hLUA_ERRFILE中定义为clc, clearvars, close all FS = 8000; % samples per second dt = 1/FS; % seconds per sample StopTime = 0.25; % seconds t = (0:dt:StopTime-dt)'; % seconds %% Sine wave: Fc = 125; % hertz x = sin(2*pi*Fc*t); % Plot the signal versus time: subplot(121); plot(t,x); xlabel('time (in seconds)'); title('Signal versus Time, frequency=125'); L=length(x); NFFT = 2^nextpow2(L); % Next power of 2 from length of y Y = (fft(x,NFFT)/L); f = FS/2*linspace(0,1,NFFT/2+1); % Plot single-sided amplitude spectrum. subplot(122);stem(f,2*abs(Y(1:NFFT/2+1))) title('Single-Sided Amplitude Spectrum') xlabel('Frequency (Hz)') ylabel('Amplitude') 因此RuntimeWarning: DateTimeField Item.updated_at received a naive datetime (2018-05-01 12:35:18.213471) while time zone support is active. RuntimeWarning) 1}}等于7(在Lua 5.3中;其他版本可能不同)。

因此,您似乎没有正确指定文件名,否则脚本无法读取。尝试使用QFile或std :: ifstream打开文件并将其打印到控制台,以确保您正确指定路径。

相关问题