How to create a project from existing files in Qt creator?

I never used Qt creator to create projects for source file management because I am scared by the mysterious .pro, .pro.user, Makefile Makefile.Debug, Makefile.Release, and the build directories Qt creator creates for me. I just used gcc command line to compile and link. After all, keep simple is my principle.

But today, I have to resort to qt creator to debug my app to fix a complex problem. I do not want to add output statements everywhere in the source files any more. I have to import the existing source files to a Qt creator project so that I can debug them in Qt creator’s debug environment. I do not want to bother to create a new project from scratch and add the existing files to the project one by one. How to create a new project based on existing files? There is a menu item under New/import project/import existing project that seems to meet my needs, but it actually does not work except to generate some mysterious files. Searching on google for a while I got a command: qmake -project, which can create a .pro file based on the files in the directory where the command is executed. That sounds great. The .pro file is like the following:

TARGET = myapp

# Input
SOURCES += myapp.cpp


I can open the .pro file with Qt creator and browse the source files now. However, when I build this projects, a lot of errors occur. First, it claims “No such file or directory” for many include files. This problem is simple to fix. I copied my -I options from the gcc command lines to the .pro as INCLUDEPATH += C:\Qt531\5.3\mingw482_32\include  C:\Qt531\5.3\mingw482_32\include\QtGui ….

Then the compiling process was successful, but the linking process failed with a bunch of errors like “undefined reference to…”. I am familiar with this error, which is caused by not setting the linking libraries correctly. Again, I copied the linked libraries from the gcc command line to the project file as LIBS += -lQt5Core -lQt5Gui -lQt5Widgets …  -L C:\Qt531\5.3\mingw482_32\lib

Then something strange happened, the linker complains “cannot find -lQt5Widgets531″,”cannot find -lQt5Network531″, etc. Note that I did not specify a lib called Qt5Widgets531 or Qt5Network531. The lib names I specified have not version suffix. How does Qt creator append the version number suffix (531) to the name of the libraries. I could not figure out the reason even after doing a lot of search on google.

After adjusting for many times the parameters and the variables in .pro file, I finally gave up using the LIBS and the INCLUDEPATH variables to build my project. After researching the qmake project file syntax and specification, I begin to be aware that  using the LIBS and the INCLUDEPATH is not the correct way to build a qt application. They are used to build non-qt programs or add non-qt libraries and header files. To build a qt program, you  should add the qt option to the CONFIG variable which enables the support for linking with basic qt libraries. You should also specify the extra qt libraries that your app uses in the QT variables. Also note that the QT variable contains the name of qt modules such as “widgets xml”(case sensitive), not the name of qt libraries. So the correct .pro file is as follows:

TARGET = myapp
CONFIG += qt debug
QT += widgets xml webkit webkitwidgets network
SOURCES += myapp.cpp


No need to add LIBS and INCLUDEPATH. qmake automatically adds the correct -I, -l, and -L options in the generated Makefile using the information provided in the QT variable and the qt option in the CONFIG variable. Now I can build the qt program without an error.

Posted in

Comments are closed, but trackbacks and pingbacks are open.