Function of ifndef/define/endif in the header file, ifndefendif
Function of ifndef/define/endif in the header file in c, c ++
I discussed a small program with my dormitory students today and found that I don't know much about it ······
It is about some of the parts in the header file:
For example, to write the header file test. h
Write two lines at the beginning of the header file:
# Ifndef _ TEST_H
# Define _ TEST_H // the file name is generally capitalized.
············
············
Write a line at the end of the header file:
# Endif
I probably didn't learn it before. I don't really understand it here. Why should I use it? I checked it online just now. I learned from some experts and wrote it here. What are the mistakes? Please point out
1. For example, if you have two C files, both of them 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.
For example:
Assume that your project contains four files, a. cpp, B. h, c. h, and d. h.
A. the cpp header is:
# Include "B. h"
# Include "c. h"
The headers of B. h and c. h are:
# Include "d. h"
In d. h, class D is defined.
In this way,
The compiler compiles. when using cpp, first according to # include "B. h "to compile B. h. Based on B. # include "d. h ", to compile d. h. class D in h is compiled;
Then, according to. the second sentence of cpp # include "c. h ", to compile c. h. d. class D in h, but class D has been compiled before, so a redefinition error will be reported. When ifndef/define/endif is added, this type of redefinition error can be prevented.
Therefore, 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 <ID>
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 the vc link, the I repeated definition error occurs, and the c compilation is successful.
Cause:
(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 ). change the extension of the source program file. after c, VC compiles the source program according to the C language syntax, instead of 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 connection results of C and C ++ are different. during compilation, C ++ may set the global variable as a strong symbol by default. Therefore, the connection error occurs. The C language determines the strength based on the initialization.
(Refer to solution:
(1). Change the source program file extension to. c.
(2). Recommended Solution: Only extern int I is declared in. h;
Define in. cpp
# Ifndef _ X_H __
# Define _ X_H __
Extern int I;
# Endif/_ X_H _ int I;
Note: variables are generally not defined in. H files.
Recommended: http://www.cnblogs.com/roucheng/p/cfenge.html