Conditional compilation and c ++ Conditional compilation
1. ifdef
#ifdef XXXXX......#endif
If XXXXX is defined, compile the above Code.
2. ifndef
#ifndef XXXXX......#endif
If XXXXX is not defined, compile the above Code.
3. if
#if XXXXX.....#endif
If XXXXX is defined and the value is true, compile the above Code.
4. if define
#if define(XXXXX)......#endif
Define XXXXX first. If the defined XXXXX value is true, compile the above Code. It is equivalent to the combination of # if and # define.
Example: prevent repeated header file references
In a project with multiple files, the following methods are often used to prevent errors caused by multiple inclusion of the same head file.
#ifndef _HEADFILE_H#define _HEADFILE_H#include
What is Conditional compilation in C ++? Can you give an example?
Generally, all the rows in the source program are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met, that is, to specify the compilation conditions for a part of the content. This is "Conditional compilation ". Sometimes, you want to compile a group of statements when a condition is met, and compile another group of statements when the condition is not met.
The most common form of Conditional compilation commands is:
# Ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif
It is used to compile program segment 1 when the identifier has been defined (generally defined using the # define command). Otherwise, compile program segment 2.
The # else part can also be absent, I .e:
# Ifdef
Procedure 1
# Denif
The "program segment" can be a statement group or a command line. This Conditional compilation can improve the universality of the C source program. If a C source program runs on different computer systems, there are some differences between different computers. For example, we have a data type. In Windows, we should use the long type, while in other platforms, we should use the float representation. In this case, we often need to make necessary modifications to the source program, this reduces the versatility of the program. You can compile with the following conditions:
# Ifdef WINDOWS
# Define MYTYPE long
# Else
# Define MYTYPE float
# Endif
If you compile a program on Windows, you can add
# Define WINDOWS
In this way, compile the following command line:
# Define MYTYPE long
If the following command line appears before this set of Conditional compilation commands:
# Define WINDOW
After pre-compilation, the MYTYPE in the program is replaced by float. In this way, the source program can be used in different types of computer systems without any modification. Of course, the above is just a simple case. You can design other conditions for compilation based on this idea.
For example, when debugging a program, you often want to output some required information, but do not output this information after the debugging is complete. You can insert the following Conditional compilation segments in the source program:
# Ifdef DEBUG
Print ("device_open (% p) \ n", file );
# Endif
If you have the following command line before it:
# Define DEBUG
The value of the file pointer is output when the program is running for debugging and analysis. After debugging, you only need to delete the define command line. Some people may think that Conditional compilation can achieve this goal, that is, adding a batch of printf statements during debugging, and deleting the printf statements one by one after debugging. Indeed, this is acceptable. However, when many printf statements are added during debugging, the modification workload is huge. If you use Conditional compilation, you do not need to delete the printf statement one by one. You only need to delete the previous "# define DEBUG" command, at this time, all the Conditional compilation segments using DEBUG as identifiers make the printf statement ineffective, that is, unified control, just like a "Switch.
The following format is also used:
# Ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif
The first line is different from the first form: Change "ifdef" to "ifndef ". It is used to compile program segment 1 if the identifier is not defined; otherwise, compile program segment 2. This form is opposite to the first form.
The usage difference between the above two forms is not... the remaining full text>
What is Conditional compilation in C ++? Can you give an example?
Generally, all the rows in the source program are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met, that is, to specify the compilation conditions for a part of the content. This is "Conditional compilation ". Sometimes, you want to compile a group of statements when a condition is met, and compile another group of statements when the condition is not met.
The most common form of Conditional compilation commands is:
# Ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif
It is used to compile program segment 1 when the identifier has been defined (generally defined using the # define command). Otherwise, compile program segment 2.
The # else part can also be absent, I .e:
# Ifdef
Procedure 1
# Denif
The "program segment" can be a statement group or a command line. This Conditional compilation can improve the universality of the C source program. If a C source program runs on different computer systems, there are some differences between different computers. For example, we have a data type. In Windows, we should use the long type, while in other platforms, we should use the float representation. In this case, we often need to make necessary modifications to the source program, this reduces the versatility of the program. You can compile with the following conditions:
# Ifdef WINDOWS
# Define MYTYPE long
# Else
# Define MYTYPE float
# Endif
If you compile a program on Windows, you can add
# Define WINDOWS
In this way, compile the following command line:
# Define MYTYPE long
If the following command line appears before this set of Conditional compilation commands:
# Define WINDOW
After pre-compilation, the MYTYPE in the program is replaced by float. In this way, the source program can be used in different types of computer systems without any modification. Of course, the above is just a simple case. You can design other conditions for compilation based on this idea.
For example, when debugging a program, you often want to output some required information, but do not output this information after the debugging is complete. You can insert the following Conditional compilation segments in the source program:
# Ifdef DEBUG
Print ("device_open (% p) \ n", file );
# Endif
If you have the following command line before it:
# Define DEBUG
The value of the file pointer is output when the program is running for debugging and analysis. After debugging, you only need to delete the define command line. Some people may think that Conditional compilation can achieve this goal, that is, adding a batch of printf statements during debugging, and deleting the printf statements one by one after debugging. Indeed, this is acceptable. However, when many printf statements are added during debugging, the modification workload is huge. If you use Conditional compilation, you do not need to delete the printf statement one by one. You only need to delete the previous "# define DEBUG" command, at this time, all the Conditional compilation segments using DEBUG as identifiers make the printf statement ineffective, that is, unified control, just like a "Switch.
The following format is also used:
# Ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif
The first line is different from the first form: Change "ifdef" to "ifndef ". It is used to compile program segment 1 if the identifier is not defined; otherwise, compile program segment 2. This form is opposite to the first form.
The usage difference between the above two forms is not... the remaining full text>