PDA

View Full Version : Lua into Qt



Defake
11th June 2015, 18:59
I need to use lua language in my Qt-application. I add necessary files into my project and write this code:


#include "stdafx.h"
#include "podkl.h"
#include <QtWidgets/QApplication>

#pragma comment(lib, "lua52.lib")

#include <lua.hpp>

int Write(lua_State* s) {
return 0;
}

int main(int argc, char *argv[]) {
QApplication a(argc, argv);
PODKL w;
w.show();

lua_State* s = luaL_newstate();
luaopen_base(s);

lua_pcall(s, 0, 0, 0);

//lua_register(s, "Write", Write);


return a.exec();
}

11203

If I compile the program that way I have a linker message:

1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol lua_pcall referenced in function main
1>D:\Programming\VisualStudio\Projects\STUDYING\PODK L\x64\Debug\\PODKL.exe : fatal error LNK1120: 1 unresolved externals

But if I launch it this way:

//lua_pcall(s, 0, 0, 0);
lua_register(s, "Write", Write);

Then I get an error message: "Unhandled exception at 0x00007FFDA7120810 (lua52.dll) in PODKL.exe: 0xC0000005: Access violation reading location 0x0000002F0ABDA198."

I have tried to launch lua from usual console project. And I have done it. Don't know why I can't do it on Qt.

What should I do to use lua on Qt-application?
(Win 8.1, x64)

d_stranz
11th June 2015, 19:10
Your link error says that the lua_pcall method isn't found in any of the libraries you link to. Is it defined in one of the lua source files you show in your screen shot? If so, is the .obj file for that being linked in to your application?

You don't check the value of "s" returned by your call to luaL_newstate(). How do you know it isn't a NULL pointer and is the cause of the access violation?

Defake
14th June 2015, 08:10
It has to be linked. I downloaded project (.c and .h) files and .dll, .lib. .pdb. Then included it to my project. How to check if the .obj file is linked or no?

I checked the 's' with debugger. It wasn't NULL

Defake
14th June 2015, 16:02
Why when I create a simple console project, include lua files and turn it on, everything is ok.
But when I create qt project and do exactly the same, I get errors about unresolved external..
Why?? I hate it already >_<

ChrisW67
14th June 2015, 21:15
My guess is, unsurprisingly, that the program crashes because the link failed to find exported Lua symbols. Frankly, I am surprised you got an executable out at all: do a completely clean build first. Check that you have linked the lua51 library through the LIBS pro file variable ( or whatever the Visual Studio obscured equivalent is).

Defake
14th June 2015, 21:35
No, I couldn't seem to add LINK line properly.
That are my efforts:

LIBS += -L"$$PWD/lua/lua5.3.0.lib"
LIBS += -L"$$PWD/lua/" -llua5.3.0
LIBS += L"$$_PRO_FILE_PWD_/lua" -llua
LIBS += L"$$_PRO_FILE_PWD_/lua/" -llua.dll
(And other looks like this variations)


But no one works. How to do it?

d_stranz
14th June 2015, 23:02
Why when I create a simple console project, include lua files

So exactly what are you doing to "include lua files"? The header files, the .c / cpp files, what? I think that when you create your Qt project you simply aren't compiling and linking the .c / .cpp file from the lua distro that contains the unresolved call.


LIBS += L"$$_PRO_FILE_PWD_/lua" -llua
LIBS += L"$$_PRO_FILE_PWD_/lua/" -llua.dll

The files shown in your screen shot are named "lua51.lib and .dll, not "lua.lib" and "lua.dll" so these LIBS statements refer to non-existent files.

Defake
15th June 2015, 08:53
I've alreadly downloaded new version lua (5.3.0). What exactly I do now:
Create Qt project in Visual stuido. Change x32 to x64 and check if the project launches correctly.
After checking I add into the project folder (in the windows explorer) a new folder "lua" and put into this all necessary lua-files (lua .c, .h, one .hpp, .lib, .dll and .exe files). Then I create into solution explorer (in Visual Studio) a new folder "lua" and put (drag n drop) into this all files from the folder "lua" in the windows explorer.

Then I include .hpp file (that contains including lua.h, lualib.h and lauxlib.h) and create new variable lua_State.

What I have at this moment:

11211

Then it seems I have to open QtCreator and adjust .pro file, but my results I've posted above.
How to adjust it properly?

d_stranz
15th June 2015, 17:21
If you are using Visual Studio for building your project, why are you also using Qt Creator? Stick with one or the other. I use Visual Studio (only) along with the Qt VS plugin for all of my Qt projects. The only thing I use Qt Creator for is to build and run the demos that come with Qt, and that only because I don't want to be bothered creating a Visual Studio project for them.

Why are you putting LIB, DLL and EXE files into the VS project? What do you think Visual Studio (or Qt Creator for that matter) is going to do with them?

By the way, if you are using Visual Studio to build your own project, any LIBs or DLLs also have to be built by the Visual C++ compiler. If your Qt Creator kit is using mingw, anything compiled in Qt Creator will be incompatible with code built in VS.

Defake
15th June 2015, 21:51
Im using Qt creator to adjust my .pro file, because I can launch qmake there (And I think that it's necessary for applying changes in .pro file). I couldn't find it in Visual Studio.

I'm putting all files from the lua folder for 100% working. I will clean my project after it works.

I downloaded lua5.3.0.tar.gz, opened Visual Studio command prompt and made the files with proper commands. Anyway it works in console project.

I think the problem is in .pro file. I should link the lua library. How can I do it properly? (Maybe in Visual Studio, if it's possible)

ChrisW67
16th June 2015, 10:45
Assuming your Lua deployment folder C:\Lua contains:


Archive: lua-5.3_Win32_dll10_lib.zip
Length Date Time Name
--------- ---------- ----- ----
8433 10-30-2014 02:11 include/lauxlib.h
14734 12-27-2014 03:24 include/lua.h
191 12-23-2004 10:53 include/lua.hpp
20308 05-19-2015 11:04 include/luaconf.h
1173 02-07-2014 03:32 include/lualib.h
231936 05-20-2015 00:47 lua53.dll
30044 05-20-2015 00:47 lua53.lib
--------- -------
306819 7 files

Then this does it


TEMPLATE = app
TARGET = test
DEPENDPATH += .
INCLUDEPATH += .

INCLUDEPATH += C:/Lua/include
LIBS += -LC:/Lua -llua53

# Input
SOURCES += main.cpp



#include <QCoreApplication>
#include <lua.hpp>

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
lua_State* s = luaL_newstate();
luaopen_base(s);
lua_pcall(s, 0, 0, 0);

int result = a.exec();

lua_close(s);

return result;
}

Defake
16th June 2015, 15:32
I couldn't find where I can use .pro file in Visual Studio (It creates .pro file, but then no using it).

So I tried to do it with Qt Creator. Qt finds libraries,


TEMPLATE = app
TARGET = newluaQt
DESTDIR = ../Win32/Debug
QT += core widgets gui
CONFIG += debug
LIBS += -L"$$_PRO_FILE_PWD_/lua/" -llua5.3.0
DEFINES += WIN64 QT_DLL QT_WIDGETS_LIB
INCLUDEPATH += ./GeneratedFiles \
. \
./GeneratedFiles/Debug \
./lua
DEPENDPATH += .
MOC_DIR += ./GeneratedFiles/debug
OBJECTS_DIR += debug
UI_DIR += ./GeneratedFiles
RCC_DIR += ./GeneratedFiles
include(newluaQt.pri)

How do i realise: I tried to launch with "-llua5.3" for example. And Qt said me "Can't find lua5.3 lib", then I tried "-llua5.3.0" and Qt said me again about my linker error. How can I fix it? I'm already trying to solve it by a couple of weeks

ChrisW67
16th June 2015, 21:38
And Qt said me "Can't find lua5.3 lib", then I tried "-llua5.3.0" and Qt said me again about my linker error.
Qt does not issue any of these messages. Your linker issues these messages because it was invoked with insufficient information to allow it to find the library. Once qmake has created the Makefile to your specification Qt is not involved in the process of compilation/linking.


We cannot see what your library file is called or where it lives. Look at my example, work out the quite obvious pattern, and do the equivalent in your pro file.

Is the name of your library file lua5.3.0.lib/lua5.3.0.dll? No, then adjust the pro file accordingly.
Do you even have a compiled library to work with? I simply downloaded the precompiled library zip file to generate my example.
Does your .lib file live in a directory called "lua" in the same place as the pro file? No, then adjust the pro file accordingly.
Does _PRO_FILE_PWD_ expand to the value you think it does? Try an absolute path instead.
Does newLuaQt.pri change any of these relevant variables?

Try understanding the actual generated compiler command that fails and how that relates to your pro file.

Defake
17th June 2015, 09:02
We cannot see what your library file is called or where it lives
I've written exactly what I have in the message above:

What exactly I do now:
Create Qt project in Visual stuido. Change x32 to x64 and check if the project launches correctly.
After checking I add into the project folder (in the windows explorer) a new folder "lua" and put into this all necessary lua-files (lua .c, .h, one .hpp, .lib, .dll and .exe files). Then I create into solution explorer (in Visual Studio) a new folder "lua" and put (drag n drop) into this all files from the folder "lua" in the windows explorer.
Then I attached a screenshot when you can see that the names are lua5.3.0.lib and lua5.3.0.dll: http://www.qtcentre.org/attachment.php?attachmentid=11211&d=1434354607



Do you even have a compiled library to work with?
Yes, I've written it here:

I downloaded lua5.3.0.tar.gz, opened Visual Studio command prompt and made the files with proper commands. Anyway it works in console project.



Does your .lib file live in a directory called "lua" in the same place as the pro file?
Yes, "I add into the project folder (in the windows explorer) [B]a new folder "lua" and put into this all necessary lua-files".
The "project folder" I mean the directory of .pro file.



Does _PRO_FILE_PWD_ expand to the value you think it does?
Everyone writes that it's directory of .pro file. I have tried both $$_PRO_FILE_PWD_ and $$PWD. Okay, I will try the absolute.

About .pri - Don't know. It's generated by Visual Studio

d_stranz
17th June 2015, 14:55
About .pri - Don't know. It's generated by Visual Studio

It's generated by Visual Studio because you told it to.

For some superstitious reason, you seem fixated on the idea that you must use both Visual Studio and Qt Creator to build your project. This mistaken idea is leading you down a road filled with confusing and conflicting assumptions about what you have to do to successfully configure your project. If you are trying to work in both environments simultaneously, it is no wonder your projects are an unbuildable mess.

If you are more familiar with Visual Studio, use Visual Studio only and forget about Qt Creator.

If you are more familiar with Qt Creator, use Qt Creator only and forget about Visual Studio.

If you don't know either one, then pick one of the two, learn it, and stick with it. Forget about the other one. You can use the same toolchain (Visual C++ compilers and linkers) with either development environment. The compilers and linkers are not the same as Visual Studio, they are external programs used by Visual Studio to build projects. Any development environment, including Qt Creator, can be configured to use the same toolchain that Visual Studio does to produce executable programs that are identical in all respects.

The Qt plugin for Visual Studio is simply a tool to help you create and edit Qt-based projects from inside of Visual Studio. Yes, it does offer a feature to create a .pro file, but this is totally unnecessary for creating and building Qt projects using Visual Studio. It is a convenience for creating a .pro file that you can use if you want to move the project out of Visual Studio and into another build environment, like Qt Creator on a Unix box. The plugin also offers a tool to import .pro files into a Visual Studio project, in case you are moving from a different environment and want to use VS instead.

Others here have shown you that they can build a QApplication-based Qt project that links to and executes lua code. Several posts ago, ChrisW67 posted both C++ code and a Qt .pro file that does exactly that.

You can use that example exactly as written in Qt Creator to build the same project, assuming you have the same directory layout as he also showed.

You can also import that .pro file into Visual Studio (and then forget the .pro file exists, because you'll be using the .vcproj (or .vcxproj, depending on VS version) from then on.

So pick one or the other as your development environment, forget about the one you don't pick, and move on. You are making it much harder than it really is.

Defake
17th June 2015, 16:46
okay, in other words if I have such .pri file:


HEADERS += ./lua/lauxlib.h \
./lua/lua.h \
./lua/lua.hpp \
./lua/luaconf.h \
./lua/lualib.h \
./newluaqt.h
SOURCES += ./main.cpp \
./newluaqt.cpp
FORMS += ./newluaqt.ui
RESOURCES += newluaqt.qrc
DEPENDPATH += .
INCLUDEPATH += .
INCLUDEPATH += C:/Lua
LIBS += -LC:/Lua -llua53

I can click "import .pri file" and everything will be ok?
No. C:/Lua - there's lua53.dll and lua53.lib and the rest. And again the linker error...

Maybe somebody can upload working project? I have just tried all that I can. I tried to follow every advise, but they weren't work. I dont know why it is so

yeye_olive
17th June 2015, 17:06
Try to break down the problem into smaller ones:

Get some project to compile and link with Lua. In order to do that, just pick ChrisW67's minimal example, and build it like you do usually, e.g. with qmake and make.
Get some project to compile and link with Lua within Visual Studio. Import ChrisW67's minimal example in Visual Studio and try to build it.
Get your own project to compile and link with Lua within Visual Studio. One possibility is to start with ChrisW67's minimal example and edit the .pro file. I can already see two potential issues with your last attempt: you wrote a .pri file, unlike ChrisW67 who wrote a .pro file. You forgot the TEMPLATE and TARGET clauses in your project file.

How far you get on this list will hint at the root cause of your problem. And remember: when someone posts a minimal, working solution, please at least try it to rule out installation/configuration/PEBKAC causes. Ignoring valuable input will not get you anywhere on this forum.

Defake
18th June 2015, 10:54
I haven't seen any working solution

yeye_olive
18th June 2015, 11:42
I haven't seen any working solution
Well, I haven't seen anyone doing my work for me either. Right now you have not told us anything about ChrisW67's minimal example. We are left to wonder in which of the following situations you are:

you have not bothered trying to import and build it, because you had rather let others take over and magically post a working version of your project, even if they do not have access to its sources and your particular configuration;
you have tried to import and build it, but it failed with some error message only you know about;
you have successfully imported and built it, but the same cannot be told of your project, due to some difference only you may know about.

I understand how frustrating it can be to be stalled by such a problem, but please do not confuse this forum with commercial support. People are willing to help you, but you have to show interest in working out a solution with all the advice they provide. Do yourself a service and start interacting with them.

d_stranz
18th June 2015, 16:57
I haven't seen any working solution

I refer you to ChrisW67's post #11 in this thread.

What more do you need? It's a complete example. Copy the code, paste it into files on your PC, make sure your Lua directory matches his, load the .pro file either into Qt Creator (directly) or into Visual Studio (using the Qt VS plugin import capability), compile it, and run it. If you can do that minimal thing (without once again trying to roll your own solution before trying this first), then go to work on modifying that example to do what you actually need to do.

ChrisW67
18th June 2015, 22:02
Just for the record, I got the Lua binaries I show from the Lua web site in precompiled form.
http://sourceforge.net/projects/luabinaries/files/5.3/Windows%20Libraries/Dynamic/
You will need to match the Microsoft compiler version and bit-ness if you use these.
If you compile your own then the paths may be different but the result the same.

I do not have VS and plugin or I would spell it it out from that perspective.

d_stranz
19th June 2015, 04:26
I do not have VS and plugin or I would spell it it out from that perspective.

Given the .pro file you posted, the Qt plugin for VS will import it and create a working VS project file just fine. One almost never hand-edits a VS project file; it's always done through the GUI.