(How to speed up the C/C++ compilation step with Visual Studio .NET 2003)

Visual C++ Pre compilation feature

Microsoft C/C++ compiler features for a long time now something called Header Precompilation, also known as PCH. You may have already encountered it or used it without knowing it.

The concept is quite simple : Why having to parse header files for each C/C++ file to compile that includes them ? For a given compilation, the header files used will not likely change and even for any subsequent compilations. Standard include files like stdio.h, stdlib.h and many system header files do not need to be parsed at each inclusion.

The C/C++ compiler uses a special set of files (default is stdafx.cpp and stdafx.h) where it can find all the files to precompile, and then reuse these pre-compiled headers in an efficient way in any future compilation. This avoids to recompile all these header files, especially the ones from the STL that can be really huge.


1. Using precompiled headers, the default way

At first, the C/C++ compiler searches for a file named stdafx.cpp and tries to compile it. This file only includes the stdafx.h file. This compilation generates a file called $(ProjectName).pch that will be used by other files being compiled to find pre-compiled symbols quickly.

Any other C/C++ file must then have a line like this one :

#include "stdafx.h"

Be aware that this line must be the very first line of your C/C++ file. Anything that you will place before will be ignored, not even parsed.

2. Using precompiled headers, from scratch

PCH feature can also be activated from a empty project, by adding files one by one. Here is the way to do this.

First, create an empty project and add 3 new files : main.cpp, stdafx.h and stdafx.cpp.

  • File stdafx.h :

    #ifndef __STDAFX_H#define __STDAFX_H#include #include #include #include #endif // __STDAFX_H
    
  • File stdafx.cpp, quite simple :

    #include "stdafx.h"
    
  • File main.cpp, also quite simple :

    #include "stdafx.h"int main(){ std::string myString("Hello, World !"); std::cout << myString.c_str() << std::endl; return 0;}
    
  • Once these files are added in the solution explorer into your C++ project, follow these steps :

  • Select the stdafx.cpp file, right click and select Properties :

    The two fields "Create/Use PCH Through File" and "Precompiled Header File" will be filled automatically.

  • Then select the project item in the solution explorer right click and select Properties :

    The two fields "Create/Use PCH Through File" and "Precompiled Header File" will be filled automatically.

    Note that changing the C/C++ properties for the project propagates them to C/C++ files that have not been customized. Here it is main.cpp, but not stdafx.cpp because we've customized settings in the previous step.

  • After setting all this, the first file to be compiled is stdafx.cpp and then the other files in the project.

    You will see that big projects compile much faster when PCH features is enabled. Also note that you can use multiple precompiled header files in one project, although it is not recommended. If you feel like you need to make multiple PCH files, it is time for you to make a static or a dynamic library.