Главная страница | назад





Article #21942: Does it really matter where I place my #include statements in C++ Builder?

Question:

Is there a recommended place to insert "#include" statements in C++Builder?

Answer:

Actually, yes.

This issue relates to the use in C++Builder of the "#pragma hdrstop" directive which terminates the list of header files eligible for precompilation in order to reduce the amount of disk space used by precompiled headers.

How does it work?

Precompiled header files can be shared between the various source files of your project only if the header files listed prior to the #pragma hdrstop are identical.  In other words, it is important to make sure the #include directives before the #pragma hdrstop are identical in all the source files, or that there are only very few variations.

In this way, you get the best compiler performance if you include common header files (e.g. #include <vcl.h>) before the #pragma hdrstop directive, while specific header files (e.g. #include "Unit1.h") should be included after it.

For example, the following code is generated when a New Application is generated: (comments added)

//---------------------------------------------------------------------------
#include <vcl.h>    // Common header file
#pragma hdrstop

#include "Unit1.h"  // Specific header file
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

Note: Use this pragma directive only in source files. The pragma directive has no effect when it is used in a header file.

Last Modified: 18-DEC-00