Difference between revisions of "Precompiled headers"
m (category fix) |
m (wikify) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | |||
[[Category:User Documentation]] | [[Category:User Documentation]] | ||
− | + | Using '''precompiled headers''' (or '''PCH''') speeds up the compilation of large projects (like Code::Blocks itself) by large amounts. This works by creating a header file which #includes all the rarely-changing header files for your project. Then the compiler is instructed to pre-compile this header file. This creates a new file which is now in a binary format that only the compiler understands. But, since it doesn't have to re-parse your header files for every source file that includes them, the process will now be considerably faster. | |
− | |||
− | + | ==Creating a precompiled header== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
To create a precompiled header for your project, just create a new header file. | To create a precompiled header for your project, just create a new header file. | ||
Say you named it "pch.h". Put the following in it: | Say you named it "pch.h". Put the following in it: | ||
Line 47: | Line 38: | ||
</pre> | </pre> | ||
+ | ==Marking a header for precompilation== | ||
− | |||
Now this file is ready to be marked as PCH. To do this, find the file in the ProjectManager tree, right-click on it and select "Properties". | Now this file is ready to be marked as PCH. To do this, find the file in the ProjectManager tree, right-click on it and select "Properties". | ||
Line 54: | Line 45: | ||
Click "Compile file" and make sure it's checked. | Click "Compile file" and make sure it's checked. | ||
− | Do | + | |
+ | Do '''not''' click "Link file". | ||
+ | |||
Also, set the priority weight to zero, to force it to be compiled before all other files (default priority is 50 - the lower this number, the higher the priority). | Also, set the priority weight to zero, to force it to be compiled before all other files (default priority is 50 - the lower this number, the higher the priority). | ||
Exit this dialog by clicking "OK". | Exit this dialog by clicking "OK". | ||
==Using a precompiled header== | ==Using a precompiled header== | ||
+ | |||
You 're almost there. Only thing left is to actually include this file so that it can be used. | You 're almost there. Only thing left is to actually include this file so that it can be used. | ||
There are two ways to do this. | There are two ways to do this. | ||
Line 68: | Line 62: | ||
-include "pch.h" | -include "pch.h" | ||
</pre> | </pre> | ||
+ | |||
The first line will emit a warning when building your project, if the PCH is _not_ used. It's nice to know this. | The first line will emit a warning when building your project, if the PCH is _not_ used. It's nice to know this. | ||
− | The second line, does all the magic: it's like adding <tt>#include "pch.h"</tt> at the top of each of your source files, except you don't have to edit them | + | |
+ | The second line, does all the magic: it's like adding <tt>#include "pch.h"</tt> at the top of each of your source files, except you don't have to edit them. | ||
+ | |||
This doesn't work in all situations, but it's the quick way to add PCH in your project. Specifically, it won't work if the PCH is in the same directory as the project file. If that's the case, go to "Project->Properties" and set the PCH mode to "Generate PCH in a directory alongside the original file" (first option). | This doesn't work in all situations, but it's the quick way to add PCH in your project. Specifically, it won't work if the PCH is in the same directory as the project file. If that's the case, go to "Project->Properties" and set the PCH mode to "Generate PCH in a directory alongside the original file" (first option). | ||
− | |||
− | |||
− |
Latest revision as of 11:10, 10 July 2006
Using precompiled headers (or PCH) speeds up the compilation of large projects (like Code::Blocks itself) by large amounts. This works by creating a header file which #includes all the rarely-changing header files for your project. Then the compiler is instructed to pre-compile this header file. This creates a new file which is now in a binary format that only the compiler understands. But, since it doesn't have to re-parse your header files for every source file that includes them, the process will now be considerably faster.
Creating a precompiled header
To create a precompiled header for your project, just create a new header file. Say you named it "pch.h". Put the following in it:
#ifndef PUT_A_UNIQUE_NAME_HERE #define PUT_A_UNIQUE_NAME_HERE // #include your rarely changing headers here #endif
replacing PUT_A_UNIQUE_NAME_HERE with something unique. Also #include your rarely-changing headers in that file, e.g.
#ifndef PUT_A_UNIQUE_NAME_HERE #define PUT_A_UNIQUE_NAME_HERE #if ( !defined(WX_PRECOMP) ) #define WX_PRECOMP #endif // basic wxWidgets headers #include <wx/wxprec.h> #ifndef WX_PRECOMP #include <wx/wx.h> #endif // #include other rarely changing headers here #endif
Marking a header for precompilation
Now this file is ready to be marked as PCH. To do this, find the file in the ProjectManager tree, right-click on it and select "Properties".
Click "Compile file" and make sure it's checked.
Do not click "Link file".
Also, set the priority weight to zero, to force it to be compiled before all other files (default priority is 50 - the lower this number, the higher the priority). Exit this dialog by clicking "OK".
Using a precompiled header
You 're almost there. Only thing left is to actually include this file so that it can be used. There are two ways to do this.
- Include it in every source file of your project (_not_ header files, only source files like *.cpp). This *must* be the very first C token in the file. In other words, put it really first. Only comments are harmless before it.
- Go to "Project->Build options" and add the following in "Compiler->Other options":
-Winvalid-pch -include "pch.h"
The first line will emit a warning when building your project, if the PCH is _not_ used. It's nice to know this.
The second line, does all the magic: it's like adding #include "pch.h" at the top of each of your source files, except you don't have to edit them.
This doesn't work in all situations, but it's the quick way to add PCH in your project. Specifically, it won't work if the PCH is in the same directory as the project file. If that's the case, go to "Project->Properties" and set the PCH mode to "Generate PCH in a directory alongside the original file" (first option).