Using Precompiled Headers

Precompiled headers (PCH) are a performance feature supported by some compilers to compile a stable body of code, and store the compiled state of the code in a binary file. During subsequent compilations, the compiler will load the stored state, and continue compiling the specified file. Each subsequent compilation is faster because the stable code does not need to be recompiled.

qmake supports the use of precompiled headers on some platforms and build environments, including:

  • Windows
    • nmake
    • Visual Studio projects (VS 2008 and later)
  • OS X and iOS
    • Makefile
    • Xcode
  • Unix
    • GCC 3.4 and above

Adding Precompiled Headers to Your Project

The precompiled header must contain code which is stable and static throughout your project. A typical precompiled header might look like this:

// Add C includes here

#if defined __cplusplus
// Add C++ includes here
#include <stdlib>
#include <iostream>
#include <vector>
#include <QApplication> // Qt includes
#include <QPushButton>
#include <QLabel>
#include "thirdparty/include/libmain.h"
#include "my_stable_class.h"

Note: A precompiled header file needs to separate C includes from C++ includes, since the precompiled header file for C files may not contain C++ code.

Project Options

To make your project use precompiled headers, you only need to define the PRECOMPILED_HEADER variable in your project file:


qmake will handle the rest, to ensure the creation and use of the precompiled header file. You do not need to include the precompiled header file in HEADERS, as qmake will do this if the configuration supports precompiled headers.

The MSVC and g++ specs targeting Windows (and Windows CE) enable precompile_header by default.

Using this option, you may trigger conditional blocks in your project file to add settings when using precompiled headers. For example:

precompile_header:!isEmpty(PRECOMPILED_HEADER) {

Notes on Possible Issues

On some platforms, the file name suffix for precompiled header files is the same as that for other object files. For example, the following declarations may cause two different object files with the same name to be generated:

SOURCES            = window.cpp

To avoid potential conflicts like these, give distinctive names to header files that will be precompiled.

Example Project

You can find the following source code in the examples/qmake/precompile directory in the Qt distribution:


The following image displays the mydialog.ui file in Qt Creator Design mode. You can view the code in the Edit mode.


/* Add C includes here */

#if defined __cplusplus
/* Add C++ includes here */

# include <iostream>
# include <QApplication>
# include <QPushButton>
# include <QLabel>


#include <QObject>

class MyObject : public QObject


#include <iostream>
#include <QDebug>
#include <QObject>
#include "myobject.h"

    : QObject()
    std::cout << "MyObject::MyObject()\n";


void util_function_does_nothing()
    // Nothing here...
    int x = 0;


#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include "myobject.h"
#include "mydialog.h"

int main(int argc, char **argv)
    QApplication app(argc, argv);

    MyObject obj;
    MyDialog dialog;

    dialog.connect(dialog.aButton, SIGNAL(clicked()), SLOT(close()));;

    return app.exec();

CONFIG   += console precompile_header
CONFIG   -= app_bundle

# Use Precompiled headers (PCH)

HEADERS   = stable.h \
            mydialog.h \
SOURCES   = main.cpp \
            mydialog.cpp \
            myobject.cpp \
FORMS     = mydialog.ui

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.