Skip to main content Skip to navigation

Precompiled headers in Eclipse


Eclipse is a very powerful development environment for C++, but has a significant drawback that the CDT development tooling environment it does not explicitly support precompiled headers which can significantly speed up compilation. GCC support for precompiled headers is defined here.

The following shows how this can be done

Precompiled headers involve having a header file (e.g. pch.h) that contains the common includes that are used by many of the source files,

#include <thread>
#include <unordered_map>
#include <mutex>
#include <sstream>
#include <stdio.h>

and then arranging starting each source file with:

#include "pch.h"
#include "otherHeaderFile.h"

The precompiled header file should be called pch.h.gch or should be in a directory called pch.h.gch.The latter allows you to have different precompiled headers for different build variants (debug/release). The compiler searches all of the files in the directory (irrespective of name) looking for one that matches the compile type.

Making it work in Eclipse

The approach borrows from the technique used by Microsoft Visual Studio where precompiled header support is built in. In this example, the header to be precompiled is called pch.h. The header file can take any name.

First, create the pch.h file with the appropriate includes, and include it in the source files as necessary. Next, create a file called pch.cpp in the same directory as pch.h. Its purpose is to allow the precompiled header to be built and should just contain one line:

#include "pch.h"

Right click on the file, select properties, select all configurations, and then select the 'Cygwin C++ compiler' Tool setting, and remove the '${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT}' entries from the expert settings, such that it then looks something like:


Then go to the miscellaneous option select the release configuration and add "-x c++-header -o <filepath>\pch.h.gch\pch.h.release" to the other flags entry where the <filepath> is a relative path to the directory containing the pch.h file. This should be done for each build configuration, changing the file suffix as appropriate. This is the config that tells gcc to build a precompiled header, and where to put it.



The build process will now create the appropriate precompiled header, but will then fail at the linking stage because it cannot find a pch.o file associated with the pch.cpp file. Once the precompiled header file has been created, pch.cpp should be excluded from the build to allow linking to work. If the header file is changed then the cpp file will need to be included in the build again (temporarily) in order to rebuild the precompiled header.


If you want to check that the precompiled header is being used, comment out some/all of the text in the original pch.h once the precompiled header has been built and the .cpp file excluded from the build. The build should proceed without errors as it uses the pre-compiled header file. The Eclipse background file checker does not use the precompiled header, and the Eclipse GUI will start showing up lots of errors against the source code.