I have installed C++SDK that have Qt but when I try compiling a code linking QApplication it gives me the error:
Error QApplication: no such file or directory
How do I link these libraries? I searched into the directories and there is a file named QApplication.h; So I tried to link it with -I (linking the directory) but it was still giving me that error.
To start things off, the error
QApplication: no such file or directory means your compiler was not able to find this header. It is not related to the linking process as you mentioned in the question.
-I flag (uppercase i) is used to specify the include (headers) directory (which is what you need to do), while the
-L flag is used to specify the libraries directory. The
-l flag (lowercase L) is used to link your application with a specified library.
But you can use Qt to your advantage: Qt has a build system named qmake which makes things easier. For instance, when I want to compile main.cpp I create a main.pro file. For educational purposes, let’s say this source code is a simple project that uses only
QDeclarativeView. An appropriate .pro file would be:
TEMPLATE += app QT += gui declarative SOURCES += main.cpp
Then, execute the
qmake inside that directory to create the Makefile that will be used to compile your application, and finally execute
make to get the job done.
On my system this
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_DECLARATIVE_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/qt_47x/mkspecs/linux-g++ -I. -I/opt/qt_47x/include/QtCore -I/opt/qt_47x/include/QtGui -I/opt/qt_47x/include/QtDeclarative -I/opt/qt_47x/include -I/usr/X11R6/include -I. -o main.o main.cpp g++ -Wl,-O1 -Wl,-rpath,/opt/qt_47x/lib -o main main.o -L/opt/qt_47x/lib -L/usr/X11R6/lib -lQtDeclarative -L/opt/qt_47x/lib -lQtScript -lQtSvg -L/usr/X11R6/lib -lQtSql -lQtXmlPatterns -lQtNetwork -lQtGui -lQtCore -lpthread
Note: I installed Qt in another directory –>
Edit: Qt 5.x and later
QT += widgets to the .pro file and solve this problem.
Answered By – karlphillip
Answer Checked By – Senaida (BugsFixing Volunteer)