Thursday, January 13, 2005

Compile your C++ Windows applications faster with pre-compiled header files

I guess I should be shameful by not knowing about this Visual Studio feature until now.

Most of the time, your C++ Windows applications requires several Windows-specific header files in order to use WINAPI functions, such as windows.h, tchar.h, or wininet.h. These header files significantly increases compilation time. Moreover, if you have more than one cpp files which all reference to the same header files such as windows.h, the compiler compiles the same header files again and again for each cpp files when producing object code. This is such a waste of time. Typically, compiling a single such cpp file takes about 2 seconds on my P4-2.6GHz PC.

Fortunately, Visual Studio C++ allows you to "cache" these common header during compilation by using the "pre-compiled header files" feature. For most programmers who use ATL or MFC to create new projects, the pre-compiled header feature is configured automatically for them. In case if you are a native WinAPI lover like me, or if you are just curious, below are the steps for configuring this feature manually:

1. Create a .cpp file and a .h file and name it whatever you want. These files are usually called StdAfx.cpp and StdAfx.h if they are generated automatically by VC++.

2. Suppose we name the files as StdAfx.cpp and StdAfx.h. For StdAfx.cpp file, type in this single line of code:
#include "StdAfx.h"

3. for StdAfx.h, type in all Windows header files you wish to include in all your source files in the project. For example:
#ifndef __STDAFX_H__
#define __STDAFX_H__
#include "tchar.h"
#include "windows.h"
#include "wininet.h"
#endif

4. On the left-hand project pane, right-click the project and click "Properties". Then click "C/C++" and then "Precompiled Headers".

5. Setup the options like this:
Create/Use Precompiled Header -- Use Precompiled Header (/Yu)
Create/Use PCH Through File -- StdAfx.h
Precompiled Header File -- $(IntDir)/$(TargetName).pch
... This will "link" all other source files to the pre-compiled header file during compilation to avoid multiple compilation of the same Windows header file.

6. Next, on the left-hand project pane, right-click StdAfx.cpp and click "Properties". Then again click "C/C++" and then "Precompiled Headers".

7. Setup the options like this:
Create/Use Precompiled Header -- Create Precompiled Header (/Yc)
Create/Use PCH Through File -- StdAfx.h
Precompiled Header File -- $(IntDir)/$(TargetName).pch
... This will notify the compiler to look for StdAfx.cpp and create the pre-compiled header file (.pch) before it compiles any other source files.

8. On all your other source files, you need to add this line as the first line of the code:
#include "StdAfx.h"

8. Save the project and rebuild.

During the rebuild, you'll notice that the compiler first picks up StdAfx.cpp, compiles it, and then creates the .pch file. After that, the other source files should compile much faster than before. To me, the compilation speeds up by at least 80%.

1 comment:

Anonymous said...

Oh! This really saves a lot of time! Seems there was no book teaching this tips whilst we were learning how to use VS.

your old buddy