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
/////////
It is very important to use # ifdef and # ifndef in the header file to prevent double definition errors.
For example, you define a class AAA in the header file AAA. h as follows:
Class aaa
{
};
If the # include "AAA. H "(not directly, or two different header files may contain this header file), because the same class cannot be defined twice. Slightly modify AAA. h:
# Ifndef _ AAA _
# DEFINE _ AAA _
Class aaa
{
};
# Endif
This problem can be avoided. If you have already included this file, _ AAA _ will be defined. If the # ifndef condition is false, the subsequent class definition will not be executed.
# Ifdef and # endif must be used in pairs.
Theoretically, it can appear anywhere (In header files and implementation files)
To prevent the header file from being contained multiple times, it is necessary to use it in the header file:
For example: # ifndef my_head_h // the header file must start with an arbitrary name. Do not conflict with other header files.
(1) header file Declaration
# Endif // end of the header file
Sometimes, "A. H" is included in B. H"
Include "B. H" and include "A. H" in "C. H ",
In this case, if ifndef/endif is not used, it will contain a. h twice and generate an error.
Conditional compilation: Compile if conditions are met. Otherwise, it will not be compiled. If you want a piece of code and sometimes do not want to compile it (for example, for debugging), you can write it like this:
# If 1 (0)
Xxxxxx
Xxxxxx
# Endif
Write 1 if needed, and change to 0 if not.
# 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;