How to write a Qt .pro file?

Qt project file(.pro file) is used to control the build of projects. I seldom use that because I’m scared by so many mysterious macros. But if you do not want to know the marcos in depth, you can write a .pro file easily by applying and modifying a template. Let me show you a typical template for a simple project. This project(myproject) has a library(mylib) and an app(myapp) that uses that library.  myproject/ is the top directory. All mylib files including source files and header files are put in myproject/mylib/. All myapp files including source files and header files are put in myproject/myapp/. We’ve written  all the c++ code, we can write the .pro files now.

First, create a .pro file under myproject/ called myproject.pro, which contains:

TEMPLATE = subdirs

CONFIG  += ordered

SUBDIRS  = mylib/mylib.pro \
myapp/myapp.pro

OTHER_FILES += README.md

Note that “subdirs” is a keyword, which means this .pro is for managing sub-directories. The .pro files for every sub-directories are listed in SUBDIRS(one .pro file name per line, the lines are concatenated with a “\”).

Now we need to write a .pro file for each sub-directory.

For mylib/, the mylib.pro is as follows:

QT       += core network

QT       -= gui

CONFIG   += c++11

TARGET    = mylib

TEMPLATE  = lib

isEmpty(INSTALL_PREFIX) {
  unix: INSTALL_PREFIX = /usr
  else: INSTALL_PREFIX = $$top_srcdir
}

win32: {
  DEFINES    += "mypar=myval"
}

include(mylib.pri)

macx: {
  QT_CONFIG  -= no-pkg-config
  DEFINES    += ...
}

unix: {
  CONFIG     += create_pc create_prl no_install_prl link_pkgconfig
}

LIBS += -L D:/dev/lib -l other.lib
INCLUDEPATH += F:/dev/otherlib/include/

 

You can add/delete Qt modules through QT macro. Note that  “core”,”network” are Qt module names, not library names. qmake will automatically introduce libraries(lib name/include files,etc.) according to this macro. TARGET  is the generated lib name.   TEMPLATE   must be set to “lib” because the .pro file is for a library. You can set different values of DEFINES, CONFIG(which will be passed to g++ as options) for different OSes. If your project uses extra external libraries, you should use LIBS+= and  INCLUDEPATH += to specify their libraries(name/directory) and include paths.  Note that we do not list src file in this .pro file. Instead, we use mylib.pri to specify the source files for this library and include mylib.pri in the .pro file. The .pri file mylib.pri is very straight-forward, which lists all source files and header files used for this library:

SOURCES  += \

file1.cpp  \

file2.cpp

HEADERS  += \

header1.h \

header2.h

The .pro for myapp/ is myapp.pro as follows:

QT       += core network

QT       -= gui

TARGET    = myapp

CONFIG   += console c++11

TEMPLATE  = app

HEADERS  += myapp.h \
util.h

SOURCES  += myapp.cpp \
main.cpp \
util.cpp

INCLUDEPATH     += $$top_srcdir/../lib
LIBS            += -L$$top_srcdir/../lib \
-L../lib \
-lmylib

The TARGET marco is the app name, TEMPLATE  must be set to “app” which means an executable program. Here we list the header and source files directly in the .pro file. As the app uses the library above, we should list its include path, lib path, and lib name using INCLUDEPATH  and LIBS.

Now you can open myproject.pro in Qt Creator and start to build the library and the app. Qt Creator will create a directory at the same level of the directory that myproject.pro resides(../build-%{CurrentProject:Name}-%{CurrentKit:FileSystemName}-%{CurrentBuild:Name}). In this case, the directory name is build-myproject-unknown-Debug. Note that myproject is got from the file name:myproject.pro, i.e., the project name is the file name of the .pro file. You will find the sub-directories  mylib and myapp are also created. Qt Creator knows this by the SUBDIRS macro in myproject.pro, not the TARGET in the .pro files for the sub-dirs, nor the .pro file names in the sub-directories. For each sub-dir, two sub-dirs(debug and release) are created to store the compiling files.

The Makefiles are generated as follows.

In build-myproject-unknown-Debug, only one Makefile is generated called Makefile. In each subdir, three Makefiles are generated. For example, in mylib/, the Makefiles are Makefile.mylib(the suffix is got from the .pro file name – mylib.pro in the sub-dir, not the TARGET in mylib.pro, nor the directory name of the sub-dir). Makefile.mylib.Debug, Makefile.mylib.Release.

If you go to build-myproject-unknown-Debug and execute mingw32-make(without a target), it will go to build-myproject-unknown-Debug/mylib and execute mingw32-make -f Makefile.mylib without a target, and eventually call mingw32-make -f Makefile.mylib.Debug(also without a target).

 

Posted in

Comments are closed, but trackbacks and pingbacks are open.