C ++ Conditional compilation

Source: Internet
Author: User

7.3 Conditional compilation command

In general, all program lines of the source program are compiled to generate the target code. However, in some special cases, you may only want to compile some program rows that meet the conditions. This is Conditional compilation.
Programmers can add some debugging statements in the debugging program to achieve tracking purposes. After the program is debugged, use Conditional compilation to re-compile the program so that the debugging statement does not participate in compilation to generate efficient code.
Common Conditional compilation commands have the following formats:
1. Format 1
# Ifdef <identifier>
Procedure 1
# Else
Procedure 2
# Endif
Ifdef, else, And endif are keywords. <Segment 1> and <Segment 2> are composed of several pre-processing commands or C ++ statements. The function of the Conditional compilation command is: if the specified <identifier> is defined in the program, <program segment 1> is used for compilation; otherwise, <program segment 2> is used for compilation.
If else branch is omitted in this format, you can directly write it as follows:
# Ifdef <identifier>
Program Section
# Endif
2. Format 2
# Ifndef <identifier>
Procedure 1
# Else
Procedure 2
# Endif
Or
# Ifndef <identifier>
Program Section
# Endif
Ifndef, else, And endif are all keywords. The function of the Conditional compilation command is: if the <identifier> is defined at the end of the program, <program segment 1> is used for compilation; otherwise, <program segment 2> is used for compilation.
3. Format 3
# If <constant expression 1>
Procedure 1
# Elif <constant expression 2>
Procedure 2
......
......
# Elif <constant expression n>
Program section N
# Else
Program section N + 1
# Endif
Among them, If, Elif, else, And endif are all keywords. The function of the Conditional compilation command is to calculate the values of constant expressions in sequence. When the logic is true, use the corresponding program segment for compilation. If the values of all constant expressions are logical false, use the program segment after else for compilation.
Visual c ++ also provides a function-like operator defined () in the following format:
Defined (<identifier>)
Its function is: when the <identifier> has been defined and has not been canceled, its expression value is not 0; when the <identifier> last definition or has been canceled, the expression value is 0. It is often used to determine whether an identifier has been macro-defined.
Conditional compilation is widely used in programming. For example, some program debugging statements (mainly conditional judgment statements and output statements) are often inserted in the source program during program debugging ). These statements are inserted to help program debugging. After debugging, you must delete them one by one. Manual deletion is troublesome and error-prone. In this case, the Conditional compilation command can be used for automatic processing. The Code is as follows:
# Define debug 1
......
......
# If debug
Cout <"OK! "<Endl;
# Endif
......
......
# If debug
If (x <0)
Cout <"error: x <0" <Endl;
# Endif
......
......
In this example, the program segments between # If and # endif are specially used for program debugging. When the program debugging is complete, cancel these statements. In this case, you can change the macro definition of DEBUG:
# Define debug 0
During re-compilation, because the expression is logical false, the debugging program segment between the original # If and # endif will no longer participate in compilation.


The macro assert defined in the header file "assert. H" can be used to test the expression value. If the expression value is 0, assert outputs an error report and calls the function abort in the stdlib. H function of the function library to terminate the execution of the program.
Assert is a very useful debugging tool that can be used to test whether a variable has a correct value. For example, assuming that the variable in the program should be greater than 10, you can use assert to test the value of X and output an error report when the value of X is incorrect. The statement used is:
Assert (x <= 10 );
If X is greater than 10, an error report containing the row number and file name is printed and the execution of the program is terminated. In this way, programmers can focus on the relevant code.
If the symbolic constant ndebug is defined, the subsequent assert will be ignored. Therefore, if you no longer need assert, you can use the following code lines:
# Define ndebug
Insert the source program. You do not need to manually delete the assert.
For example, when applying for dynamic memory .....
Assert assert

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.