Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Compilation

Similar presentations


Presentation on theme: "Conditional Compilation"— Presentation transcript:

1 Conditional Compilation
printf() statements cab be inserted code for the purpose of displaying debug information during program testing. Once the program is debugged and accepted as "working'', it is desirable to remove these debug statements. Of course, if later an undetected bug appears during program use, we would like to put some or all debug statements back in the code to pinpoint and fix the bug.

2 Conditional Compilation
One approach to this is to simply "comment out'' the debug statements; i.e. surround them with comment markers, so that if they are needed again, they can be "uncommented''. This is a vast improvement over removing them and later having to type them back. However, this approach does require going through the entire source file(s) to find all of the debug statements and comment or uncomment them

3 Conditional Compilation
Use conditional compilation to select particular sections of code to compile, while excluding other sections. Conditional compilation statements are designed to run during compile time, not at run time.

4 Conditional Compilation
A pre-processor directive in C is identified by the presence of a #symbol before the statement. The pre-processor substitutes language code for all the directives, and passes the newly inflated file to the compiler. We would expect, using conditional compilation, that some code would be left out, but this is not always the case

5 Conditional Compilation Directives
Cause the preprocessor to conditionally suppress the compilation of portions of source code. These directives test a constant expression or an identifier to determine which tokens the preprocessor should pass on to the compiler and which tokens should be bypassed during preprocessing.

6 Conditional Compilation Directives
The directives are: #if and #elif - compare the value of constant_expression to zero: #ifdef - checks for the existence of macro definitions #else - If the condition specified in the #if, #ifdef, or #ifndef directive evaluates to 0, and the conditional compilation directive contains a preprocessor #else directive, the lines of code located between the preprocessor #else directive and the preprocessor #endif directive is selected by the preprocessor to be passed on to the compiler.

7 Conditional Compilation Directives
The directives are: #ifndef - checks whether a macro is not defined #endif - ends the conditional compilation directive

8 Conditional Compilation Directives
The preprocessor conditional compilation directive spans several lines: The condition specification line (beginning with #if, #ifdef, or #ifndef) Lines containing code that the preprocessor passes on to the compiler if the condition evaluates to a nonzero value (optional) The #elif line (optional) The preprocessor #endif directive For each #if, #ifdef, and #ifndef directive, there are zero or more #elif directives, zero or one #else directive, and one matching #endif directive

9 Conditional Compilation Directives
Example: Suppose we want to create a project with a debug build, by using conditional compilation to include statements for debug output: #define a debug macro at the start of the file, and then test for it before evaluating debug statements. If this technique spans multiple files, it would probably be useful to define a compiler flag -DDEBUG, rather than have to include the #define statement at the start of each file.

10 Conditional Compilation Directives
Once defined, we could then use this debug flag as follows: #ifdef DEBUG printf("x is %d\n", x); #endif

11 Conditional Compilation Directives
Preventing Multiple #includes using Conditional Compilation To avoid conflicts that occur when the pre-processor #includes the same header file twice, and there are duplicate definitions of certain constants, macros or functions, the following code can be used: #ifndef HEADER_H #include "header.h" #define HEADER_H #endif


Download ppt "Conditional Compilation"

Similar presentations


Ads by Google