Using Precompiled Headers | Qt 4.8 (original) (raw)

Precompiled headers 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 (PCH) on some platforms and build environments, including:

Contents of the Precompiled Header File

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

Example: stable.h

#if defined __cplusplus

#include #include #include #include // Qt includes #include #include #include "thirdparty/include/libmain.h" #include "my_stable_class.h" ... #endif

Note that 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 PCH, you only need to define the PRECOMPILED_HEADER variable in your project file:

PRECOMPILED_HEADER = stable.h

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 PCH.

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) { DEFINES += USING_PCH }

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:

PRECOMPILED_HEADER = window.h SOURCES = window.cpp

To avoid potential conflicts like these, it is good practice to ensure that header files that will be precompiled are given distinctive names.

Example Project

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

mydialog.ui

MyDialog 0 0 401 70 Mach 2! 9 6 Join the life in the fastlane; - PCH enable your project today! - &Quit Alt+Q qPixmapFromMimeSource

stable.h

#if defined __cplusplus

include

include

include

include

#endif

myobject.h

#include

class MyObject : public QObject { public: MyObject(); ~MyObject(); };

myobject.cpp

#include #include #include #include "myobject.h"

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

util.cpp

void util_function_does_nothing() {

int x = 0;
++x;

}

main.cpp

#include #include #include #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()));
dialog.show();

return app.exec();

}

precompile.pro

TEMPLATE = app LANGUAGE = C++ CONFIG += console precompile_header

Use Precompiled headers (PCH)

PRECOMPILED_HEADER = stable.h

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