Code::Blocks下GCC预编译的实现

本文介绍如何使用预编译头文件(PCH)来加快大型项目的编译速度。通过创建包含较少变动的头文件并预编译这些文件,可以显著减少编译时间。文章详细说明了设置过程,包括创建预编译头文件、标记它进行预编译及在项目中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Precompiled headers

From CodeBlocks

Jump to: navigation, search

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".

Image:pch-1.png

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.

  1. 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.
  2. 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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值