For example: to write a header file test.h
#ifndef _test_h
#define _TEST_H//is typically an uppercase file name
············
#endif
1. For example, you have two C files, and all two C files include the same head file. At compile time, the two C files will be compiled together into a running file, so the problem is, a lot of declaration conflicts.
Suppose you have 4 files in your project, respectively, A.cpp,b.h,c.h,d.h. &NBSP
A.cpp Head is:
#include "B.h"  
#include "c.h"  
The heads of B.h and c.h are:
#include "d.h"  
and D.h has the definition of class d. &NBSP
This way, the
compiler compiles the a.cpp by compiling b.h based on #include "B.h" and then according to B.h " D.h ", to compile the d.h of this file, so that the d.h inside the class d compiled; &NBSP
then according to A.cpp's second sentence #include " c.h ", To compile c.h, eventually will find the d.h inside the class d, but the class d has been compiled before, so will report a redefinition error. Plus ifndef/define/endif, can prevent this redefinition error.
so the contents of the head file are placed in #ifndef and #endif. &NBSP
If your header file will be referenced by multiple files, you should add this. &NBSP
The general format is this:
#ifndef < logo >
#define < logo >
...
...
#. The endif < identity >
can theoretically be freely named, but the "identity" of each header file should be unique. The named rule for the identity is typically the header filename is all uppercase, underlined, and the "." in the file name. Also becomes underlined, such as: stdio.h
#ifndef _stdio_h_
#define &NBSP;&NBSP;_STDIO_H_&NBSP;&NBSP;&NBSP
...
#endif
2. Define the problem with the variable in the #ifndef (generally do not define the variable in #ifndef).
#ifndef AAA
#define AAA
...
int i;
...
#endif
There is a variable definition in the VC link when I repeat the definition of error, and in C successfully compiled.
Reason:
(1). When you first generate. obj with a. cpp file that uses this. h header, int i is defined inside; when another one uses this. cpp again [separately] generates. obj, int I was also defined and then two obj was added by another. CPP also include this header, and a duplicate definition appears.
(2). After changing the source program file extension to. C, VC compiles the source program according to the C language syntax instead of C + +. Because in C, if you encounter multiple int i, you automatically think that one is a definition, and the other is a declaration.
(3). C and C + + language connection results are different, may (guess) when compiling, C + + language will be the global variable defaults to strong symbol, so the connection error. C language is based on whether the initialization of the strong and weak judgments.
(Refer to the workaround:
(1). Change the source program file name extension to. C.
(2). Recommended solution:. h only declare extern int i; and defined in. cpp
#ifndef __x_h__
#define __x_h__
extern int i;
#endif//__x_h__ int i;
Note The problem: variables are not generally defined in. h files.
Http://blog.sina.com.cn/s/blog_74a45938010191b7.html