How does c++ hashinclude work?

  • 2020-04-01 21:29:15
  • OfStack

You may not be familiar with it, or you may not have cared about it. We only care about whether the program works correctly, or how the program is implemented, and so on.

To introduce the familiar but not so familiar "#include," let's first take a look at C/C++ header files.

The header file provides a centralized location for related declarations. Header files typically contain class definitions, extern variable declarations, and function declarations. Note the difference between declarations and definitions here: the essential difference is that definitions can occur only once and declarations can occur multiple times. The declaration does not allocate space, but the definition allocates space. Proper use of header files ensures that all files use the same declaration of a given entity. When the declaration needs to be modified, only the header file needs to be updated.

The header file can also be defined as the cosnt object and the inline function whose values are known at compile time. The above entities are defined in the header file because the compiler needs their definition to generate code. For example, in order to generate object code that defines or USES a class, the compiler needs to know the data members that make up the type and the corresponding functions.

In C++, there are places where you need to put constant expressions. For example, the initialization of an enumeration member must be a constant expression. A constant expression is one that the compiler can evaluate at compile time. When a const integer variable initializes itself through a constant expression, the const integer variable may be a constant expression. For a const variable to be a constant expression, the initialization must be visible to the compiler. In order for multiple files to use the same constant value, the const variable and its initialization must be visible to each file. So by putting its definition in a header file, the compiler knows to initialize the const constant whenever it is used.

How does #include work?

The #include facility is part of the C++ preprocessor.

The preprocessor handles the source code of the program, which is run before the compiler.

#include accepts only one parameter: the header file name.

The preprocessor replaces each include with the contents of the specified header file.

Our own header file is stored in the file. The system's header file may be saved in a more efficient compiler-specific format.

Header files often #include other header files. Header file defined entities are often used in other file facilities. So design the header file so that it can be included in the same source file multiple times. We must ensure that including the same file does not cause the classes and objects defined by the header file to be defined multiple times. To make the header file safe to pass, use the preprocessor to define the header file protection.

What is a header protection?

Just look at the following and you'll see

Before writing the header file, we need to introduce some additional preprocessor facilities. The preprocessor allows us to customize variables. To avoid name collisions, preprocessor variables are often represented in all caps. Preprocessing variables have two states: defined or undefined.
 
#ifndef BEGEIN_H 

#define BEGEIN_H 

//Define some classes or define some functions
#endif 


If you don't have two header files defining and using the same preprocessor variable, then there's no problem. If this happens, you can avoid the problem of multiple names of preprocessor variables by naming the preprocessor variables with the entity classes defined in the header file.

# include < > This is different from #include ""
If the header file name is in < > Is considered a standard header file. The compiler looks for the header file in a predefined location, and if it is "" it is considered a non-system header file, which usually starts in the path of the source file.

Related articles: