pch.htm

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

HTM
785
字号
<P>

Since you probably perform an incremental make 10 times more often than you do a full build, it seems wise to keep the

incremental make time down, even if it means that a full build will be 10% slower. This is the approach that I take. I

do not define the <TT>PRECOMPILE_ALL</TT> value in any of my projects.

</P>

<B>Don't remove existing <TT>#include</TT> statements:</B> Creating a common header file does not mean that you should remove

include statements from your header files and C++ source files. Leave those include statements where they are. There are several

reasons why you should leave existing include statements. First, if you remove include statements from your header files,

C++Builder will simply add them back again. Second, you may want to stop pre-compiling certain files, which would force to you

add the include statements back into your source files. Lastly, by leaving include statements intact, you preserve the necessary

inclusion order between header files. If you remove include statements, you will need to worry about the order that you list include

statements in your common header.

</P>

<P>The include files prevent against multiple inclusion, so you don't need to worry about including the same file twice. The code

below is taken from the mainform of the project from the Results section. It shows the include statements that remain in the

source, even though the common header already includes them.

</P>

<pre>

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

<font color="navy">// MAINFORM.CPP</font>

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

<font color="green">#include "pch.h"</font>

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



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

<font color="green">#include "mainform.h"</font>

<font color="green">#include "About.h"</font>

<font color="green">#include "util.h"</font>

<font color="green">#include "claim.h"</font>

<font color="green">#include "expert.h"</font>

<font color="green">#include "vendor.h"</font>

<font color="green">#include "lawfirm.h"</font>

<font color="green">#include "registry.h"</font>

<font color="green">#include "exceptions.h"</font>

<b>...</b>

<b>...</b>

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

<font color="navy">// MAINFORM.H</font>

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

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

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

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

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

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

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

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

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

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

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

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



<font color="green">#include "defs.h"</font>



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

<b>class</b> TMDIParent <b>:</b> <b>public</b> TForm

<b>{</b>

<b>__published</b><b>:</b>

<b>...</b>

</pre>

<P>

<P>

The point of showing this code is to demonstrate that even though I use a common include file called <TT>PCH.H</TT>,

I don't remove existing include statements from my source files. When I create a new source file, I add include

statements so that file compiles without relying on includes from the common header. Once the new source file compiles,

I insert an include statement for the common header to keep compile times down.

</P>

<P>

<B>Observe case sensitivity for include statements:</B> Someone posted a comment on the C++Builder IDE group that the compiler

observes case sensitity when matching pre-compiled images. If you include the common header with varying case in different units, the

compiler will regenerate the pre-compiled image for each one. The next code example demonstrates what you should not do.

</P>

<pre>

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

<font color="navy">// MAINFORM.CPP</font>

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

<font color="green">#include "pch.h"</font>

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

<b>...</b>



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

<font color="navy">// ABOUT.CPP</font>

<font color="green">#include &lt;vcl.h>     </font><font color="navy">// OK. same case as mainform.cpp</font>

<font color="green">#include "pch.h"</font>

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

<b>...</b>



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

<font color="navy">// SPLASH.CPP</font>

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

<font color="green">#include "PCH.H"     </font><font color="navy">// WRONG. mismatched case</font>

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

<b>...</b>



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

<font color="navy">// LOGON.CPP</font>

<font color="green">#include &lt;Vcl.h>     </font><font color="navy">// WRONG. mismatched case</font>

<font color="green">#include "pch.h"</font>

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

<b>...</b>

</pre>

<P>

In this example, the compiler will generate and use the same pre-compiled image for <TT>MAINFORM.CPP</TT> and <TT>ABOUT.CPP</TT>,

but <TT>SPLASH.CPP</TT> and <TT>LOGON.CPP</TT> will each generate their own pre-compiled image, which will slow down the compile

time. The rule of thumb is this: every include file listed above the <TT>#pragma hdrstop</TT> directive should use the same case that

other files use. Include statements below the <TT>#pragma hdrstop</TT> directive don't have to match case, because they are not

pre-compiled.

</P>

<P>

<B>Consider adding VCL.H to the common header:</B> The common header that was used in the example code for technique 2

does not include the file <TT>VCL.H</TT>. Each CPP source file includes both <TT>VCL.H</TT> and <TT>PCH.H</TT>, like

this.

</P>

<pre>

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

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

<font color="green">#include "pch.h"</font>

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

</pre>

<P>

You may prefer to include <TT>VCL.H</TT> from within the common header. If you do, then each CPP file can simply

include the common header.

</P>

<pre>

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

<font color="green">#include "pch.h"</font>

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

</pre>

<P>

This is cleaner, and less prone to error because you don't have to worry about which file should be listed first.

However, it violates the suggestion from the note <I>don't remove existing <TT>#include</TT> statements</I>. If you

ever need to yank out the common header file, you will need to add <TT>VCL.H</TT> back into every CPP file in your

project.

</P>



<P>

<B>Use a separate CSM file for each project:</B> By default, C++Builder creates a common pre-compiled header file

called vcl50.csm in the <TT>$(BCB)\lib</TT> directory (the name of this file is different with each version of

C++Builder). C++Builder will share this pre-compiled header file among all of your projects. In order to take

advantage of the techniques in this article, you should configure your projects to create and use their own

pre-compiled image file.

</P>

<P>

You can do tell your project to use its own pre-compiled header file by specifying a file name for the pre-compiled

image. This option is on the Compiler tab of the Project-Options dialog box. Change this value from

<TT>$(BCB)\lib\vcl50.csm</TT> to a filename that won't conflict with other projects, such as <TT>pch.csm</TT>.

</P>

<P>

There are several advantages to using a separate <TT>CSM</TT> file for each project. First, it allows you to create

a customize your common header file for each project. Secondly, because the <TT>CSM</TT>

file in the lib directory is shared, it tends to grow in size. It is not uncommon to have a shared

<TT>CSM</TT> file that is larger than 30 MB. Lastly, some users have reported that sharing the same <TT>CSM</TT> across

multiple projects is a source of phantom compiler errors. If you are getting strange compiler errors in

<TT>algorithm.h</TT>, you might be able to solve the problem by not sharing your <TT>CSM</TT> files across multiple

projects.

</P>

<P>

<B>Count the number of #00 files in your project directory:</B> When C++Builder generates a

pre-compiled header image, it

saves that image to a file with a <TT>.CSM</TT> extension. It will also save one or more files with an extension of

<TT>.#??</TT> (ie <TT>#00</TT>, <TT>#01</TT>, <TT>#02</TT>). The number of <TT>.#??</TT> files depends on how well you

have optimized your pre-compiled headers. If you optimize them perfectly, C++Builder will generate a single file with

an extension of <TT>.#00</TT>. If you don't optimize your files correctly, you will see other files with similar file

extenstions (<TT>.#01</TT>, <TT>.#02</TT>, etc). The presence of additional <TT>#00</TT> files indicates that you

have not optmized your headers correctly. 

</P>

<P>

So what are these files? For each unique pre-compiled image in the <TT>.CSM</TT> file, C++Builder generates one file

with a <TT>.#??</TT> extension, starting with <TT>.#00</TT>. These files are usually between 1 and 2 MB's in size. When

you have optimized your files perfectly, the <TT>.CSM</TT> file will contain one and only one pre-compiled image. As a

result, you end up with one <TT>.CSM</TT> file and one file with a <TT>.#00</TT> extension. When you don't

optimize your projects, the <TT>.CSM</TT> file may contain many unique pre-compiled images. Each unique image generates

one additional <TT>.#??</TT> file.

</P>

<P>

Here are a couple of additional tips regarding these mysterious <TT>.#00</TT> files. C++Builder generates these files

as its needs them, but it never deletes them. Because these files are fairly large, you should delete them every so

often. Also, because C++Builder never deletes them, you may see <TT>.#00</TT> files even after you optimize your

pre-compiled headers. Third, these files are created in the same directory that the <TT>CSM</TT> file resides. By default,

this is the <TT>$(BCB)\lib</TT> directory. Lastly, you may want to take a look in your <TT>$(BCB)\lib</TT> and count how many of these files are lying

around. In the previous note, we talked about how C++Builder generates one common <TT>.CSM</TT> file in the lib directory

that all projects use. This shared <TT>.CSM</TT> file tends to be big, and it also tends to contain many different

pre-compiled images. Since each unique image generates a separate <TT>.#00</TT> file, you end up with tons of

<TT>.#00</TT> files in your lib directory. Currently, my BCB4 lib directory contains five of these files (

<TT>vcl40.#00</TT>,<TT>vcl40.#01</TT>, <TT>vcl40.#02</TT>, <TT>vcl40.#03</TT>, and <TT>vcl40.#04</TT>).

</P>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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