Conditional compilation of preprocessing commands (# ifdef, # else, # endif, # If)

Source: Internet
Author: User

Conditional compilation of pre-processing commands (# ifdef, # else, # endif, # If, etc)


Preprocessing is the work done before the first lexical scan and syntax analysis of compilation. To put it bluntly, it is to process the preprocessing part before compiling the source file, and then compile the processed code. The advantage of doing so is that the processed code will become very short.
For the file inclusion (# I nclude) and macro definition (# define) in the pre-processing command, the detailed description has been provided in the book and will not be detailed here. Here we mainly describe Conditional compilation (# ifdef, # else, # endif, # If, etc. There are three situations:

1: Case 1:
# Ifdef _ xxxx
... Program section 1...
# Else
... Program section 2...
# Endif
This indicates that if the identifier _ XXXX has been defined by the # define command, the program segment 1 will be compiled; otherwise, the program segment 2 will be compiled.

Example:
# Define num
.............
.............
.............
# Ifdef num
Printf ("previously defined num! :)/N ");
# Else
Printf ("no definition of num before! :(/N ");
# Endif
}
If the program starts with the # define num line, that is, the num is defined. When we encounter the # ifdef num below, of course the first printf is executed. Otherwise, the second printf will be executed.
In my opinion, you can easily enable/disable a specific function of the entire program.

2: Case 2:
# Ifndef _ xxxx
... Program section 1...
# Else
... Program section 2...
# Endif
# Ifndef is used here, indicating if not def. Of course, it is the opposite of # ifdef (if the identifier _ XXXX is not defined, the execution segment 1; otherwise, the execution Segment 2 ). This is not an example.

3: Case 3:
# If constant
... Program section 1...
# Else
... Program section 2...
# Endif
It indicates that if the constant is true (not 0, whatever number, as long as it is not 0), the program segment 1 will be executed; otherwise, the program segment 2 will be executed.
I think this method can be used to add the test code. To enable the test, you only need to change the constant to 1. Instead of testing, you only need to change the constant to 0.

# Ifdef # ifndef and other usage

# Ifndef

# Ifndef in the header, which is critical. For example, you have two C files, both of which include the same header file. During compilation, these two C files need to be compiled into a runable file together, so the problem arises and a large number of declarations conflict.

Put the header file content in # ifndef and # endif. Whether or not your header file will be referenced by multiple files, you must add this. The general format is as follows:

# Ifndef <ID>
# Define <ID>

......
......

# Endif

<Identifier> in theory, it can be freely named, but the "identifier" of each header file should be unique. The naming rule of the identifier is generally that the header file name is in uppercase, followed by an underscore, and the "." In the file name is also changed to an underscore, such as stdio. h.

# Ifndef _ stdio_h _
# DEFINE _ stdio_h _

......

# Endif

2. Problems with defining variables in # ifndef (generally not defined in # ifndef ).

# Ifndef aaa
# Define aaa
...
Int I;
...
# Endif
There is a variable definition in it
When I is linked in VC, an I-defined error occurs, and C is compiled successfully.

Conclusion:

(1 ). when you first use this header. CPP file generation. in OBJ, int I defines in it when another one uses this. CPP is generated [separately] again. when obj is used, int I is defined again, and then two OBJ are another. CPP also includes this header. If the header is connected together, the definition will be repeated.

(2). After the source program file extension is changed to. C, VC compiles the source program according to the C language syntax, rather than C ++. In C language, if multiple int I is encountered, one of them is automatically considered to be the definition, and the other is the declaration.

(3). the C language and C ++ language have different connection results, and the C ++ language performs global
The default value of the variable is a strong symbol, so the connection error occurs. The C language determines the strength based on the initialization. (Reference)

Solution:

(1). Change the source program file extension to. C.

(2). Recommended solutions:
Only extern int I is declared in. h; defined in. cpp

<X. h>
# Ifndef _ x_h __
# DEFINE _ x_h __
Extern int I;
# Endif/_ x_h __
<X. c>
Int I;

Note:

Variables are generally not defined in. H files.

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.