Precompiled Headers

From WxWiki
Jump to: navigation, search

Precompiled Headers in Microsoft Visual Studio C++

Set Up Precompiled Headers

Perform the following steps:

  1. Include wx/wxprec.h in all your .cpp files. (Consider adding #ifndef WX_PRECOMP magic each time.)
  2. Set the 'Procompiler Header' setting for each source file to "Use (/Yu)", except for one file, setting that to "Create (/Yc)":
    1. In Project properties → Configuration properties → C/C++ → Precompiled Headers set the following:
      1. Precompiled Header: Use (/Yu)
      2. Precompiled Header File: wx/wxprec.h
      3. Precompiled Header File: $(IntDir)\$(TargetName).pch (this is the default value)
    2. Select the properties of one of the source files, typically the main one (the one that has IMPLEMENT_APP.) In File properties → Configuration properties → C/C++ → Precompiled Headers set the following for that one file:
    3. Precompiled Header: Create (/Yu)
  3. Build your project and verify that the project builds okay and that the compile time has decreased dramatically.

Kasajian 06:06, 11 July 2010 (UTC)

Updating Precompiled Headers

After precompiled headers are set up, your project may occasionally fail to build. You may get linker errors with compile warnings, one for each .cpp file, of the form:

warning C4651: '/DWX_PRECOMP' specified for precompiled header but not for current compile

This means your project has changed so that the precompiled headers you've been using are out of date. In order to update your precompiled headers, build your projects with /Yc once again, and then change /Yc back to /Yu by repeating the steps above.

Precompiled headers in MinGW

(Beforehand I want to apologize for my broken English)

Precompiled c++ headers for Visual Studio, are explained at codeproject, similar is explained for QT ---- (emiraga)

I wasn't able to activate pch support as written in wxWidgets documentation which says that pch works with: “Visual C++ (including embedded Visual C++), Borland C++, Open Watcom C++ and newer versions of GCC.” but doesn't actually work with my GCC 3.4.2. I can't see GCC support in wxprec.h:

 // check if to use precompiled headers: do it for most Windows compilers unless
 // explicitly disabled by defining NOPCH
 #if ( defined(__WXMSW__) && 
        ( defined(__BORLANDC__)    || 
          defined(__VISUALC__)     || 
          defined(__DIGITALMARS__) || 
          defined(__WATCOMC__) ) ) || 
       defined(__VISAGECPP__) || 
     // If user did not request NOPCH and we're not building using configure
     // then assume user wants precompiled headers.
     #if !defined(NOPCH) && !defined(__WX_SETUP_H__)
         #define WX_PRECOMP

wxWidgets lib says that: “The file "wx/wxprec.h" includes "wx/wx.h". Although this incantation may seem quirky, it is in fact the end result of a lot of experimentation, and several Windows compilers to use precompilation which is largely automatic for compilers with necessary support”. I don't know what “experimentation” they talking about, but even activating WX_PRECOMP flag manually and including wxprec.h do nothing for activating pch support.

If you would like to activate pch support with GCC do the following:

First of all, you should create precompiled header itself. You can make it by typing g++ yourprec.h with the same compiler options as all your project files. In yourprec.h you can add all needed headers. Here is the example:

 #include <wx/wx.h>
 #include <wx/socket.h>
 #include <wx/notebook.h>
 #include <wx/xrc/xmlres.h>
 #include <wx/image.h>

In Dev-C++ you can create Makefile.mak file to automate pch compiling process. Here is the example of Makefile.mak:

 yourprec.h.gch: yourprec.h
 	$(CXX) -c yourprec.h -o yourprec.h.gch $(CXXFLAGS)
 all-before: yourprec.h.gch

After that you should go to Project->Project Options->Makefile and add your Makefile.mak. If you using IDE different from Dev-C++ you can add lines written above somewhere in your makefile.

After yourprec.h is compiled you'll see a file named yourprec.h.gch. Keep in mind that folders where yourprec.h and yourprec.h.gch lie should be in the compiler search paths. The last step is #including yourprec.h in all source files of your project. You can add all other necessary headers below yourprec.h (yourprec.h must be the first included file).

I should explain that GCC compiler acts this way: if it find correct .h.gch file it use it, otherwise it use .h with the same name. If the compiler is using the .h file while there is a .h.gch file in the same directory you can use -Winvalid-pch compiler flag to recognize what's wrong.


My project consists of 1000 lines of C++ code. Compiling time from 27 seconds (with wx.h) was reduced to 10 seconds (with wx.h.gch) in wxWidgets 2.4.2. And from 80 sec -> 32 sec with wxWidgets 2.6.0.


Andreev Nikita aka "Lestat"

Precompiled headers in wxDevCpp

This is experimental knowledge only...

In wxDevCpp, using MinGW and wxWidgets, i found the matter working this way:

  1. Prepare a designated precompile-header file as usual like discussed in the chapters above!
  2. Include this header file in your wxDevCpp project workspace!
  3. Configure this header file to "create precompiled header"!

(Annotation: The wxDevCpp IDE grants you the freedom to define any c, cpp or header file to "create" the precompiled header. This is bullshit. It actually DOES use this setting only when it is set on a header file. All other files are automatically set to "use" the precompiled header anyway.)

To further drastically reduce the compilation time: Include ONLY those files in the precompile-header, which are really necessary and nearly everywhere used. No file should go into that construct, which is included only once or twice throughout your project!

01:09, 9 December 2009 (UTC) White Gandalf