Pre-compilation and macro definition of C/C ++

Source: Internet
Author: User

Pre-compilation is the first step in the entire compilation process. It is the result output by the g ++-E option.

This step processes Macros in the source file/header file. Macro commands are commonly used in the following categories:

File Inclusion: # include
Macro definition: # define, # undef
Conditional compilation: # ifdef, # ifndef, # if, # elif, # else, # endif
1. File Inclusion # include
Pre-processing will include all the content of the file to be included, such as the following file prepro. cpp:

[Cpp]
# Include "prehead. h"
Int main (){
Add (1, 2 );
}
Introduced the header file prehead. h:

[Cpp]
# Ifndef _ PREHEAD_H
# Define _ PREHEAD_H
/* Declaration of a function that add two integer */
Int add (int, int );
# Endif
Use the command g ++-E prepro. cpp for pre-compilation. The output result is as follows:

[Plain]
#1 "prepro. cpp"
#1 "<built-in>"
#1 "<command-line>"
#1 "prepro. cpp"
#1 "prehead. h" 1
 
 
 
 
 
 
Int add (int, int );
#2 "prepro. cpp" 2
 
Int main (){
Add (1, 2 );
}
We can see that the macro definitions of the two files are deleted in the result, and the comments in the file prehead. h are also deleted, and the rest of the content is introduced into prepro. h. As for the description of # ifndef.

2. macro definition: # define, # undef
# One usage of define macro definition is to declare a macro to implement Conditional compilation. This usage will be introduced together with # ifdef and other macro commands.

# Another use of define macro definition is to define constants and quasi-inline functions. This method is not easy to use and should be avoided.

Use the variables defined by # define, such as: # define PI 3.14. After pre-compilation, all PI values in the program will be replaced by 3.14. Another example is the g ++ blog:

[Cpp]
# Define ONE 1
# Define TWO 2
 
Int add_one_two (){
Return ONE + TWO;
}
The pre-compiled results are as follows:

[Plain]
#1 "add. cpp"
#1 "<built-in>"
#1 "<command-line>"
#1 "add. cpp"
 
Int add_one_two (){
Return 1 + 2;
}
We can see that the defined ONE is replaced with 1, and the defined TWO is replaced with 2.

It is good to use constants, but it is not good to use the # define method because the variable names are replaced during debugging and the constant effect is lost. It is best to define variables in the following way:

[Plain]
Const TYPE VAR_NAME = value
Use # define to define the quasi-inline function. For example, the following example (derived from Objective C ++):

[Cpp]
# Define max (a, B) (a)> (B )? (A): (B ))
Generally, there is no problem,:

[Cpp]
Int a = 5, B = 0;
Max (++ a, B);/* a is automatically increased twice */
Max (++ a, B + 10);/* B auto-incrementing once */
Therefore, such definitions may cause problems. The purpose of using # define to define a function is to increase the program running speed. However, the speed of this method is limited. We should start with improving the data structure and algorithm. This kind of # define, decisively written as inline will be OK, so many brackets write tired, look tired, not necessary.

3. Conditional compilation: # ifdef, # ifndef, # if, # elif, # else, # endif
Conditional compilation is the most essential application of macros. Let's talk about the meaning of these commands first. If you have read my previous blog and insist on making questions on USACO, you must be familiar with the most basic C ++ syntax, these commands can also be used at a glance. The following is an illustration:

[Plain]
# Ifdef // if define, if defined...
# Ifndef // if not define, if not defined...
# If // if...
# Elif // or if...
# Else // or...
# Endif // end if
First condition Compilation
It is used to prevent one header file from being introduced twice. For example, start the header file prehead. h In the example:

[Cpp]
# Ifndef _ PREHEAD_H/* If _ PREHEAD_H */is not defined */
# Define _ PREHEAD_H/* Definition _ PREHEAD_H */
/* Declaration of a function that add two integer */
Int add (int, int );
# Endif/* end the above # ifndef */
Then change our prepro. cpp file and add a line # include "prehead. h"

[Cpp]
# Include "prehead. h"
# Include "prehead. h"
Int main (){
Add (1, 2 );
}

Here I will expand it manually to the following, and then analyze it in the comment:
[Cpp]
# Ifndef _ PREHEAD_H/* If _ PREHEAD_H * is not defined */
# Define _ PREHEAD_H/* Definition _ PREHEAD_H * // * define it */
/* Declaration of a function that add two integer */
Int add (int, int );
# Endif/* end the above # ifndef */
 
# Ifndef _ PREHEAD_H/* If _ PREHEAD_H * is not defined, the condition is false */
# Define _ PREHEAD_H/* Definition _ PREHEAD_H * // * from here, endif will be deleted by the Preprocessor */
/* Declaration of a function that add two integer */
Int add (int, int );
# Endif/* end the above # ifndef * // * the subsequent compilation is independent of the second # ifndef _ PREHEAD_H */
 
Int main (){
Add (1, 2 );
}
Use the command g ++-E prepro. cpp to output the same as in the first example, as shown below:

[Plain]
#1 "prepro. cpp"
#1 "<built-in>"
#1 "<command-line>"
#1 "prepro. cpp"
#1 "prehead. h" 1
 
 
 
Int add (int, int );
#2 "prepro. cpp" 2
 
Int main (){
Add (1, 2 );
}

Maybe you will say that this SB, a header file is included twice, Hahahaha. However, this is just an example. In actual projects, file introduction is complicated, such as prepro. cpp introduces the third file, and then the third file introduces prehead. h, so if there is no Conditional compilation, prepro. cpp contains two preheads. h.

Use # ifndef... # define... # endif can ensure that only one file is introduced once... it can be anything, as long as you can ensure that the defined content is not defined elsewhere, but the Convention is to define the file name in uppercase and then add a few underscores, as in the above example.

Second condition Compilation
It is used for cross-platform compilation. For example:

[Cpp]
# Ifdef _ linux _
// Code related to the linux platform...
# Endif
# Ifdef _ windows _
// Windows platform related code...
# Endif
# Ifdef _ macos _
// Mac OS platform code
# Endif

Then, in the config. in h, if the compiler is defined as # define _ linux _, then the compiler only compiles the Code related to the linux platform. If it is defined as # define _ windows _, compile only the relevant code on the windows platform.

The configuration file config. h is not manually written, but automatically generated by the script. Writing scripts or using tools to generate scripts is another topic. Let's talk about it later.

 

There are other macro commands, but few commands are used, so I will not write them. For more information, see this Website: Click to enter. This website provides top-notch C ++ documentation, manuals, and help. Learn more about C ++.


References:

Valid tive C ++, third edition. Scott Meyers. 2005

Excerpted from programmers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.