pch.htm

来自「C++builder学习资料C++builder」· HTM 代码 · 共 785 行 · 第 1/4 页

HTM
785
字号
to speed up compilation of the second source file. Imagine the performance boost that you would attain if the project

contain 50 source files instead of 2.

</P>



<BR>

<H3>

<A NAME="vcl">Explanation of Pre-Compiled Headers in a VCL GUI Project</A>

</H3>

<P>

The use of pre-compiled headers in the previous example reduced the build time of the project by almost 50%. But that

was a simple console mode program that didn't do much. You probably want to know how you can take

advantage of pre-compiled headers in a full blown VCL GUI program. By default, C++Builder automatically turns on

pre-compiled headers for you. However, C++Builder does not pre-compile every header file that is used by your program.

It only pre-compiles the file <TT>VCL.H</TT>, which you can see by inspecting the top of any form's source file:

</P>

<pre>

<font color="green">#include &lt;vcl.h></font>

<font color="green">#pragma hdrstop</font>

</pre>

<P>

The <TT>#pragma hdrstop</TT> directive tells the compiler to stop generating the pre-compiled image. Any

<TT>#include</TT> statement located before the <TT>hdrstop</TT> directive will be pre-compiled, while any

<TT>#include</TT> below the directive will not be pre-compiled.

</P>

<P>

So how many header files get pre-compiled when <TT>VCL.H</TT> is pre-compiled? If you look at <TT>VCL.H</TT>, you will

see that it includes another file called <TT>VCL0.H</TT> (assuming you have BCB3). If you don't alter the default

settings of C++Builder, <TT>VCL0.H</TT> will include a small set of VCL header files. They are:

</P>

<pre>

<font color="navy">// Core (minimal) VCL headers</font>

<font color="navy">//</font>

<font color="green">#include &lt;sysdefs.h></font>

<font color="green">#include &lt;system.hpp></font>

<font color="green">#include &lt;windows.hpp></font>

<font color="green">#include &lt;messages.hpp></font>

<font color="green">#include &lt;sysutils.hpp></font>

<font color="green">#include &lt;classes.hpp></font>

<font color="green">#include &lt;graphics.hpp></font>

<font color="green">#include &lt;controls.hpp></font>

<font color="green">#include &lt;forms.hpp></font>

<font color="green">#include &lt;dialogs.hpp ></font>

<font color="green">#include &lt;stdctrls.hpp></font>

<font color="green">#include &lt;extctrls.hpp></font>

</pre>

This is a small cross section of header files, and it probably represents only a subset of the header files that are

used in a moderate to large sized project. <TT>VCL0.H</TT> does allow you to pre-compile more header files through the

use of conditional defines. You can <TT>#define</TT> a variable called <TT>INC_VCLDB_HEADERS</TT> to pre-compile the

VCL database header files. Likewise, you can define <TT>INC_VCLEXT_HEADERS</TT> to pre-compile header files for the

extra controls that come with C++Builder. If you define a variable called <TT>INC_OLE_HEADERS</TT>, C++Builder will

pre-compile some of the SDK COM header files. These defines should be placed before the <TT>#include</TT> statement

for <TT>VCL.H</TT>.

</P>

<pre>

<font color="green">#define  INC_VCLDB_HEADERS</font>

<font color="green">#define  INC_VCLEXT_HEADERS</font>

<font color="green">#include &lt;vcl.h></font>

<font color="green">#pragma hdrstop</font>

</pre>

<P>

<B>Note:</B> If you decide to try this technique, make sure you add to the two defines to every CPP file, even if they

don't use DB classes or extra controls. The reasoning for this will be explained shortly.

</P>



<BR>

<H3>

<A NAME="optimize">Optimizing C++Builder's Use of Pre-Compiled Headers</A>

</H3>

<P>

The default pre-compiled header settings do reduce the time it takes to build a project. You can prove this fact by timing

a full build of a large project when pre-compiled headers are on and by timing the build when pre-compiled headers are off.

The goal of this article is to tweak the way C++Builder pre-compiles files to reduce build times even more. In this

section, I have outlined two techniques for improving build times.

</P>

<P>

Before we discuss the techniques, it important to realize how C++Builder decides that it can use an existing pre-compiled

image when compiling a source file. C++Builder generates a unique pre-compiled image for every source file in your project.

These pre-compiled images are saved in a file on your hard drive. The compiler will re-use an existing pre-compiled image

when two source files require the same pre-compiled image. This is an important distinction. Two source files will

require the same pre-compiled image if they include exactly the same files. Furthermore, they must include the

files in the same order. Simply put, the source files must be identical up until the <TT>#pragma hdrstop</TT> directive.

Here are some examples:

</P>

<PRE>

    <B>Example 1: Pre-compiled images don't match</B>

    <FONT COLOR="navy">//--------------------                  //--------------------</font>

    <FONT COLOR="navy">// UNIT1.CPP                            // UNIT2.CPP</font>

    <FONT COLOR="green">#include &lt;stdio.h&gt;                      #include &lt;iostream.h&gt;</FONT>

    <FONT COLOR="green">#pragma hdrstop                         #pragma hdrstop</FONT>





    <B>Example 2: Pre-compiled images don't match</B>

    <FONT COLOR="navy">//--------------------                  //--------------------</FONT>

    <FONT COLOR="navy">// UNIT1.CPP                            // UNIT2.CPP</FONT>

    <FONT COLOR="green">#include &lt;stdio.h&gt;                      #include &lt;stdio.h&gt;</FONT>

    <FONT COLOR="green">#include &lt;iostream.h&gt;                   #pragma hdrstop</FONT>

    <FONT COLOR="green">#pragma hdrstop</FONT>





    <B>Example 3: Pre-compiled images don't match</B>

    <FONT COLOR="navy">//--------------------                  //--------------------</FONT>

    <FONT COLOR="navy">// UNIT1.CPP                            // UNIT2.CPP</FONT></FONT>

    <FONT COLOR="green">#include &lt;stdio.h&gt;                      #pragma hdrstop   </FONT>

    <FONT COLOR="green">#pragma hdrstop                         #include &lt;stdio.h&gt;</FONT>





    <B>Example 4: Pre-compiled images match</B>

    <FONT COLOR="navy">//--------------------                  //--------------------</FONT>

    <FONT COLOR="navy">// UNIT1.CPP                            // UNIT2.CPP</FONT>

    <FONT COLOR="green">#include &lt;stdio.h&gt;                      #include &lt;stdio.h&gt;</FONT>

    <FONT COLOR="green">#include &lt;string.h&gt;                     #include &lt;string.h&gt;</FONT>

    <FONT COLOR="green">#include &lt;iostream.h&gt;                   #include &lt;iostream.h&gt;</FONT>

    <FONT COLOR="green">#include &lt;windows.h&gt;                    #include &lt;windows.h&gt;</FONT>

    <FONT COLOR="green">#include "unit1.h"                      #include "unit1.h"</FONT>

    <FONT COLOR="green">#pragma hdrstop                         #pragma hdrstop</FONT>



    <B>Example 5: Pre-compiled images match</B>

    <FONT COLOR="navy">//--------------------                  //--------------------</FONT>

    <FONT COLOR="navy">// UNIT1.CPP                            // UNIT2.CPP</FONT>

    <FONT COLOR="green">#define  INC_VCLDB_HEADERS              #define  INC_VCLDB_HEADERS</FONT>

    <FONT COLOR="green">#define  INC_VCLEXT_HEADERS             #define  INC_VCLEXT_HEADERS</FONT>

    <FONT COLOR="green">#include &lt;vcl.h&gt;                        #include &lt;vcl.h&gt;</FONT>

    <FONT COLOR="green">#pragma hdrstop                         #pragma hdrstop</FONT>



    <FONT COLOR="green">#include "unit1.h"                      #include "unit2.h"</FONT>





    <B>Example 6: Pre-compiled images don't match</B>

    <FONT COLOR="navy">//--------------------                  //--------------------</FONT>

    <FONT COLOR="navy">// UNIT1.CPP                            // UNIT2.CPP</FONT>

    <FONT COLOR="green">#define  INC_VCLDB_HEADERS              #include &lt;vcl.h&gt;</FONT>

    <FONT COLOR="green">#define  INC_VCLEXT_HEADERS             #pragma hdrstop</FONT>

    <FONT COLOR="green">#include &lt;vcl.h&gt;</FONT>

    <FONT COLOR="green">#pragma hdrstop</FONT>

</PRE>

<P>

When the compiler processes a source file with a pre-compiled image that does not match an existing image, the

compiler will produce a completely new image from scratch. Look at Example 2 above. Even though <TT>STDIO.H</TT> is

compiled along with <TT>UNIT1.CPP</TT>, the compiler will translate <TT>STDIO.H</TT> again when it compiles

<TT>UNIT2.CPP</TT>. Pre-compiled headers reduce compile times only when the compiler can re-use an existing

pre-compiled image across multiple source files.

</P>

<P>

This is the foundation for both of the techniques that I list here. Pre-compile as many header files as you can, and

make sure that you use the same pre-compiled image in every source file.

</P>

<H4>Technique 1:</H4>

<P>The first technique is to simply boost the number of files that <TT>VCL.H</TT> includes by

adding two conditional defines to every source file. Open every CPP file in the project, including the project source

file, and change the first two lines of the file so they look like:

</P>

<pre>

<font color="green">#define  INC_VCLDB_HEADERS </font>

<font color="green">#define  INC_VCLEXT_HEADERS</font>

<font color="green">#include &lt;vcl.h></font>

<font color="green">#pragma hdrstop</font>

</pre>

<P>

If you don't like the idea of adding these defines to every source file, you can accomplish the same thing by adding

<TT>INC_VCLDB_HEADERS</TT> and <TT>INC_VCLEXT_HEADERS</TT> to the conditional defines line under Project - Options -

Directories/Conditional.

</P>

<P>

You might want to throw in some of the C RTL header files that you commonly use, along with <TT>WINDOWS.H</TT>. Make

sure that you add the lines before the <TT>hdrstop</TT> pragma, and make sure that you list them in the same order in

every C++ source file.

</P>

<pre>

<font color="green">#define  INC_VCLDB_HEADERS</font>

<font color="green">#define  INC_VCLEXT_HEADERS</font>

<font color="green">#include &lt;vcl.h></font>

<font color="green">#include &lt;windows.h></font>

<font color="green">#include &lt;stdio.h></font>

<font color="green">#pragma hdrstop</font>

</pre>



<H4>Technique 2:</H4>

<P>

Technique 1 works fairly well, but it isn't very flexible. If you decide to add a new header file to the list of

files that get pre-compiled, you need to modify every C++ source file in your project. Furthermore, Technique 1 is

prone to error. If you mess up the order of your includes, you can actually make your compile times worse, not better.

</P>

<P>

Technique 2 addresses some of the downfalls of Technique 1. The strategy here is to create one huge header file that

includes every header file that is used in your project. This single file will include the VCL files, windows SDK

header files, and RTL header files. It can also include all of the header files for forms and units that you have

created, but as we will see later on, you don't want to pre-compile header files that are likely to change (see the

note entitled <I>Don't pre-compile header files that change</I>).

</P>

<P>

Here is an example of what the common header file will look like:

</P>

<pre>

<font color="navy">//---------------------------------------------------------</font>

<font color="navy">// PCH.H: Common header file</font>

<font color="green">#ifndef PCH_H</font>

<font color="green">#define PCH_H</font>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?