Included file from header / source file

What is the difference between including a file from a header file or from a C ++ source file in terms of performance at compile time?

What are the reasons for including files in the source file rather than in the header file (unless absolutely necessary)?

Does one (header) affect compilation time and the other (source file) affects link time?

+3
source share
5 answers

When you include a file anywhere, you can think of it as an extension in the file, which should then be processed by the preprocessor and compiler.

- , , , . , , .

, . , , include , .

. , .

+12

, , , . . , , , <windows.h>. WIN32_LEAN_AND_MEAN.

, : , .

:

// A.h
class A
{
    ...
}

// B.h
#include "A.h"
class B
{
    A *_a;
    ...
}

A.h, IDE , B.h, A. B.h :

// B.h
class A; // forward declaration, declared in "A.h"
class B
{
    A *_a;
    ...
}

. .

+2

- .

, , , - , , , .

+1

, . , , , . , , . , , , .

+1

To reduce the compilation time, only the headers of the source file (.cpp / other extensions) were used, because otherwise it would generate a cascade of headers. This is no longer a problem at present, and including headings where they are actually needed (even other headings) can avoid the need to include many headings every time in your sources ...

http://www.icce.rug.nl/documents/cplusplus/cplusplus07.html#an973 (for a more detailed answer)

+1
source

Source: https://habr.com/ru/post/1720727/


All Articles