Preprocessing in C ++

xiaoxiao2021-03-06  34

First, the origin of pretreatment: There are many language characteristics (especially the obscure language of the language) in the history of C , and the pre-treatment is one of them. C inherits the C language pre-processor from the C language (C language pre-processor, by Dr. Bjarne is referred to as CPP, I don't know if it is referred to as C Program Preprocessor). Second, the common pretreatment function: The main role of the preprocessor is to replace a resource through the preprocessing built-in function. The most common preprocessor is: file containing, conditional compilation, layout control and macro replacement . The file contains: #include is a most common preprocessing, mainly to be a reference combination source program body. Conditional compilation: # if, # iFNDef, # ifdef, # Endif, # undef, etc. is also a common pre-process, mainly for compile time with selective selection, comment down some specified code to achieve version control, preventive The file repeatedly included. Layout control: #pragma, which is also an important aspect of our application pretreatment, the main function is to provide unconventional control flow information for compilers. Macro replacement: #define, this is the most common usage, it can define various functions such as symbol constants, function functions, renamed, string splicing. Third, pre-processing instructions: The format of the pre-processed instruction is as follows: # Directive tokens # symbols should be the first non-empty character of this line, usually we put it in the starting position.

If the instruction can't put it, it can be controlled by / control, for example: #define error if (error) exit (1) equivalent to #define error / if (error) exit (1) but we are generally not very so used, the more common as follows: # ifdef __BORLANDC__ if_true <(is_convertible :: value)> :: template then :: type Make; # else enum {is_named = is_named_parameter :: Value}; type}; type <(is_named)> :: template the ":: TYPE_KEY_VALUE> :: Type Make; # Endif Let's take a look at the common pretreatment instruction: #define macro definition #undef unfained macro #include Text contains #ifdef If the macro is defined to compile #ifndef If the macro is not defined, the control #IF expression of the compiling #Endif ends the compiling block is compiled. #ELSE is compiled as other pre-processed residual options. #ELIF This is a combination of #LSE and #IF #LINE Change the current number of rows and file name #Error outputs an error message #pragma provides unconventional control flow information for compileries. We will take these pre-processes. One note, considering the importance and cumbersome of the macro, we put it in the end.

Fourth, the file contains instructions: This pre-processing method is the most common, usually we write programs, the most common usage is: #include file: // Standard library header file #include File: // Old-style standard library header file #include "io.h" file: // user-defined header file #include "../file.h" file: // unix under the parent directory File #include "/usr/local/file.h" File: // Unix under the full path #include "../file.h" File: // DOS under the parent directory under the parent directory under the parent directory "/ usr /LOCAL/FILE.H "File: // DOS's full path to this inside This should pay attention: 1, we use or ? We advocate , not , why? I think you may remember that I have given a few reasons, here I roughly say: First, .h format header file is abandoned by the Standard Commission in September, in September, in September, and we should follow the standard, Suitable for the development of the times. Second, iostream.h only supports narrow character sets, iostream supports narrow / wide character sets. Also, standards have made many changes in iostream, and interfaces and implementations have changed. Finally, iostream components are all put in Namespace STD to prevent name pollution. 2, and "IO.H" difference? In fact, their only difference is that the search path is different: for the #include , the compiler starts searching for the #include "IO.H" from the standard library path, the compiler starts searching for the five, compiling control instructions : The main purpose of these instructions is to make a selection of selective selection, comment out of some specified code to reach the version control, prevent the file repeatedly included in the file.

Using the format, as follows: 1, #ifdef identifier your code #ENDIF If Identifier is a defined symbol, your code will be compiled, otherwise the 2, #ifndef Identifier your code #Endifier if Identifier is an undefined symbol, Your code will be compiled, otherwise it will be removed 3, #IF expression your code #ENDIF If the expression is not zero, your code will be compiled, otherwise the excuse 4, #ifdef Identifier your code1 #Else your code2 #ENDIF If Identifier is a definition The symbol is compiled. Otherwise, Your Code2 will be compiled 5, #IF expressin1 your code #elif expression2 your code #Else Your code3 #ENIF If Epression1 is non-zero, compile your code1, otherwise, if expression2 Non-zero, compile your code2, otherwise, compile your Code3 other precompiled instructions In addition to the concentrated compilation instructions we said above, there are three uncommon compilation instructions: # line, # error, # pragma, we Next, let's talk about it. #LINE The syntax is as follows: #LINE Number filename, for example: #LINE 30 a.h where the file name A.H can be omitted. This instruction can change the current line number and file name, such as the above pre-processing instruction, can change the current line number 30, and the file name is A.H. It seems that there is nothing to use, but he is still a bit used, that is, in writing in the compiler, we know that the compiler will generate some intermediate files in the C source compilation process, and can guarantee files through this directive. The name is fixed and will not be replaced by these intermediate files, which is conducive to analysis. #Error syntax is as follows: #error info, for example: #ifndef unix #error this Software Requires the UNIX OS. #ENDIF This instruction is mainly to give an error message. The above example is that if not in the UNIX environment, it will output This Software Requires The UNIX OS. Then induce the compiler to terminate. Therefore, the purpose of this instruction is to give certain information before the program crashes.

转载请注明原文地址:https://www.9cbs.com/read-66490.html

New Post(0)