[c++] Compile preprocessing

    [c++] Compile preprocessing

        1. The file contains  #include

        2. Macro definition (macro replacement )  #define

        3. Conditional compilation directive #ifdef

refer to:

"C++ from entry to proficient" People's Posts and Telecommunications Publishing House

Detailed explanation of C/C++ preprocessor https://blog.csdn.net/huang_xw/article/details/7648117 

 

 

      The preprocessor, also known as the preprocessor , is included in the compiler. The process of C/C++ compilation system compiling program is preprocessing, compiling and linking . A preprocessor is a program that processes program source files according to preprocessing instructions before the program source files are compiled.

      The preprocessor does the first processing of the source file, it deals with preprocessing commands. There are three main preprocessing commands provided by c++:

            file contains commands

            macro definition command 

            Conditional compilation

      Preprocessor directives are identified with a # sign at the beginning and do not include a semicolon at the end. Preprocessor commands are not part of the C/C++ language itself, and they cannot be compiled and linked directly.

 

1. The file contains the #include
preprocessing directive #include is used to include the header file, and there are two forms:

    #include<xxx.h>  (angle brackets indicate that the included file is in the include subdirectory of the system directory)

    #include "xxx.h"   (The double-quote form generally indicates the header file written by the user )

    First, these header files generally exist in the include subdirectory of the C++ system directory. When the C++ preprocessor program encounters this command, it searches for the given file in the include subdirectory and embeds it in the current file. This form is also the standard form.

    The second is for header files written by users themselves. File paths and file names can be indicated in double-quote form. If the absolute path is not given in the double quotation marks, the default is the file in the user's current directory. At this time, the system first searches for the file to be included in the user's current directory, and then searches in the system directory if it cannot be found.

2. Macro definition (macro replacement ) #define

    #define is used to define macros. There are two types of macro definitions : macro definitions without parameters and macro definitions with parameters .

    (1) Macro definition without parameters

        Form : #define   identifier string  

        For example : #define PI 3.1415926

        Role : Replace a string with an identifier. The process of replacing macro names with strings at compile time is called "macro expansion".

        Advantages : Using macro definitions can reduce the workload of repeatedly writing certain strings in the program, and can also reduce the error rate.

        Scope : The effective scope of a macro definition is called the scope of the macro name, and the scope of the macro name starts from the end of the macro definition to the end of the source code file where it is located. If you need to terminate the scope of the macro name, you can use the preprocessing directive #undef to add the macro name.

Note : The macro definition only does character replacement and does not allocate memory space. Macro substitution does not take run time, only compile time.

 

(2) Macro definition with parameters

        Form : #define   macro name ( parameter list )   string

        For example : #define S(a,b) a*b

                    area=S(2,3);

        Expansion process : Replace from left to right by the string specified in the #define command.

Note : The definition of a macro with parameters is similar to a function. When calling, the formal and actual parameters correspond one-to-one, which is similar to the method of function calling. But there is an essential difference between the two: when a function is called, the value of the actual parameter expression must be obtained first, and then the formal parameter is brought in; while the macro definition is only a simple string replacement, no operation is involved.

 

       In the C language, #define is used to create constants, or define macros with parameters. In C++, these features are still retained, but macros without parameters are often replaced by const statements, and macros with parameters are replaced by inline functions.

 

3. Conditional compilation instruction #ifdef
      In general, each line in the source program must be compiled when compiling, but sometimes it is hoped that a certain part of the program will only be compiled when a certain condition is met. If this condition is not met, This part of the content is not compiled, this is conditional compilation .

      Conditional compilation is mainly to selectively select and comment out some specified codes during compilation to achieve multiple version control and prevent duplicate inclusion of files. #if, #ifndef, #ifdef, #else, #elif, #endif are relatively common conditional compilation preprocessing directives, which can determine compilation conditions based on the value of an expression or whether a specific macro is defined.
    ① Directive meaning
    #if the expression is non-zero, compile the code;
    #ifdef compile if the macro is defined;
    #ifndef compile if the macro is not defined;
    #else compile as the remaining options of other preprocessing;
    #elif This is a combination of #else and #if options;
    #endif ends control of the compiled block.
    ② Common form
    #if_#endif form:
    #if constant expression or #ifdef macro name or #ifndef macro name
       block
    #endif

    If the constant expression is true (//the macro name is defined//the macro name is not defined), then compile the following program segment; otherwise, it will not be compiled, and this program will be skipped.

-------------------------------------------         END      -------------------------------------



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817965&siteId=291194637