How to compile Qt
-
hi i just installed qt 5.7 and i can't get it to compile this sample code from the book I'm reading.
#include <QApplication> #include <QLabel> int main (int argc, char *argv[]) { QApplication a(argc, argv); QLabel label("Hello World"); return a.exec(); }
first try with gcc gives me this error:
test.cpp:1:24: fatal error: QApplication: No such file or directory #include <QApplication> ^ compilation terminated.
second time i tried to Include QtWidget using this command:
gcc -IC:\Qt\Qt5.7.1\5.7\mingw53_32\include\QtWidgets test.cpp
and got this error:
C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0x38): undefined reference to `__imp__ZN12QApplicationC1ERiPPci' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0x7e): undefined reference to `__imp__ZN6QLabelC1ERK7QStringP7QWidget6QFlagsIN2Qt10WindowTypeEE' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0x93): undefined reference to `__imp__ZN12QApplication4execEv' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0xa5): undefined reference to `__imp__ZN6QLabelD1Ev' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0xb5): undefined reference to `__imp__ZN12QApplicationD1Ev' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0xdd): undefined reference to `__imp__ZN6QLabelD1Ev' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text+0xf2): undefined reference to `__imp__ZN12QApplicationD1Ev' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text$_ZN7QStringC1EPKc[_ZN7QStringC1EPKc]+0x36): undefined reference to `__imp__ZN7QString16fromAscii_helperEPKci' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.xdata+0x10): undefined reference to `__gxx_personality_seh0' C:\Users\mohse\AppData\Local\Temp\ccSV3Iir.o:test.cpp:(.text$_ZN15QTypedArrayDataItE10deallocateEP10QArrayData[_ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x1e): undefined reference to `__imp__ZN10QArrayData10deallocateEPS_yy' collect2.exe: error: ld returned 1 exit status
PS: I'm on windows platform and use the opensource version
.
.
.
.
UPDATE:
hello everyone I've finally complied my first qt project. now I'll tell you how i did it in case you had the same problem:first : (on windows only) : open the qt command prompt shipped with qt 5 ( you can find it from start menu ).
second : go to where your source code is located and write qmake -project in command prompt and press enter.
third : open the .pro file with a text editor and add QT += widgets to the file and save and exit.
fourth : now run qmake with pro file as argument and finally run make (mingw32-make.exe on windows).the executable should be at release folder made by make.
-
Hi and welcome to devnet forum
I would recommend to use Qt creator as well. This will make things much easier for you.
What you are doing so far is using the Qt includes, but you need to link also with the Qt libs.
Do you have installed Qt libs through the online installer?
In this case you shall be able to add also the tool Qt creator. -
You did not tell the linker which lib to use. You just give a folder.
Especially for larger projects you need to use qmake to generate makefiles. Those makefiles are used with a make tool (in your case mingw-make). All that you are trying to avoid. In your small example this might be managable but later with a bit larger project it becomes a nightmare.
When you are starting out with Qt creator all this is managed by Qt creator and you have also templates at hand.
An input for qmake would be a .pro file like this here (untitled.pro):
QT += core QT -= gui CONFIG += c++11 TARGET = untitled CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += test.cpp
The makefile could be generated with :
qmake.exe untitled.pro -spec win32-g++ "CONFIG+=debug"
NOTE: I am not sure if this is going to work since there might some additional path settings required. Therefore, I am recommending the installation of Qt creator.
-
Hi and welcome to devnet,
You are missing
CONFIG += widgets
in your .pro file. -
Ooops. Sorry grabbed wrong template. This is actually for a console application.
@nullbuil7
here is the one created for gui application which should be more suitable thenQT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = untitled TEMPLATE = app SOURCES += test.cpp HEADERS += FORMS +=
This one is simply created with Qt creator from the internal templates.
-
@nullbuil7 Are you using qmake or cmake for your project file?
While you can use gcc manually that sounds like a really painful way to compile Qt applications.
If you don't have a project file that would probably be the issue. You can create one with qmake by doing
qmake -project
in the root of your source dir.Then to build it is
qmake
then runmake
. You will pretty much never usegcc
by itself in any real programming. It would take a lot of typing to a build an application with that as you will see once you see the make output. -
@ambershark
using that method gives this .pro :###################################################################### # Automatically generated by qmake (3.0) Mon Jan 9 06:58:04 2017 ###################################################################### TEMPLATE = app TARGET = Documents INCLUDEPATH += . # Input SOURCES += main.cpp
and this make error:
main.cpp:1:24: fatal error: QApplication: No such file or directory #include <QApplication> ^ compilation terminated. make: *** [main.o] Error 1 g++ -c -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I/opt/Qt5.7.1/5.7/gcc_64/include -I/opt/Qt5.7.1/5.7/gcc_64/include/QtGui -I/opt/Qt5.7.1/5.7/gcc_64/include/QtCore -I. -I/opt/Qt5.7.1/5.7/gcc_64/mkspecs/linux-g++ -o main.o main.cpp Makefile:568: recipe for target 'main.o' failed
and after trying to do it in ubuntu by issuing this command :
g++ main.cpp -fPIC -std=c++11 -I /opt/Qt5.7.1/5.7/gcc_64/include -I /opt/Qt5.7.1/5.7/gcc_64/include/QtWidgets -o test
i get this error:
/tmp/ccgf1Yf7.o: In function `main': main.cpp:(.text+0x43): undefined reference to `QApplication::QApplication(int&, char**, int)' main.cpp:(.text+0x83): undefined reference to `QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)' main.cpp:(.text+0x9b): undefined reference to `QWidget::show()' main.cpp:(.text+0xa0): undefined reference to `QApplication::exec()' main.cpp:(.text+0xae): undefined reference to `QLabel::~QLabel()' main.cpp:(.text+0xba): undefined reference to `QApplication::~QApplication()' main.cpp:(.text+0xed): undefined reference to `QLabel::~QLabel()' main.cpp:(.text+0xfe): undefined reference to `QApplication::~QApplication()' /tmp/ccgf1Yf7.o:(.qtversion[qt_version_tag]+0x0): undefined reference to `qt_version_tag' /tmp/ccgf1Yf7.o: In function `QString::QString(char const*)': main.cpp:(.text._ZN7QStringC2EPKc[_ZN7QStringC5EPKc]+0x36): undefined reference to `QString::fromAscii_helper(char const*, int)' /tmp/ccgf1Yf7.o: In function `QTypedArrayData<unsigned short>::deallocate(QArrayData*)': main.cpp:(.text._ZN15QTypedArrayDataItE10deallocateEP10QArrayData[_ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x1e): undefined reference to `QArrayData::deallocate(QArrayData*, unsigned long, unsigned long)' collect2: error: ld returned 1 exit status
so basically It's a linker error. i still don't know how to fix it.
-
@nullbuil7 Ok add this line to your pro file:
QT += gui widgets
rerun qmake and make again, should work this time.