/*
#ifndef _stdafx_h
#define _stdafx_h
#include <iostream.h>//cin cout
#include <malloc.h>//malloc Free
#include <string.h>//strcpy
#include <stdlib.h>//exit ()
#endif
*/
(1) Content: Write two lines at the beginning of the header file (StdAfx.h):
#ifndef _stdafx_h
#define _STDAFX_H//is typically an uppercase file name
············
············
Write a line at the end of the header file:
#endif
(2) Role: The main purpose is to prevent the duplication of the header file containing and compiling
For example:
Suppose you have 4 files in your project, namely A.cpp, B.h, C.h, D.h.
A.cpp's 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 int a, which is a. As a result, compiler compiled a.cpp, first according to #include "b.h" to compile b.h this file, and then according to B.h inside #include "d.h", to compile the d.h of this file, so that the d.h inside the int a compiled; and then according to A.cpp's The second sentence #include "c.h", to compile the c.h, will eventually find the d.h inside of the int a, but the int a has been compiled before, so will report a redefinition error.
Add IFNDEF/DEFINE/ENDIF to prevent this redefinition error.
(3) There is another point to note:
When other files, such as main.cpp, use StdAfx.h and other function libraries, be aware of where the function library is placed
Such as:
Main.cpp:
#include <stdio.h>
#include "StdAfx.h"
.........
Errors occur at run time .... Reason:
The compiler uses a precompiled header file StdAfx.h through a header file. The compiler believes that all code before the instruction #include "stdafx.h" is precompiled, skipping #include "stdafx.h" instructions and using PROJECTNAME.PCH to compile all the code after this instruction.
Therefore, all MFC implementation files The first statement is: #include "stdafx.h". All the code in front of it will be ignored, so the other headers should be included behind this line. Otherwise, you will get a "nosuchfileordirectory" that makes you baffled by the wrong hints.
So put the #include "StdAfx.h" in the front (the right form below)
Main.cpp:
#include "StdAfx.h"
#include <stdio.h>
.........