Talking about the function of #ifndef~ #define ~ #endif ~

Source: Internet
Author: User

/*
#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>

.........

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.