Turn: the role of #ifdef and #endif in C + +

Source: Internet
Author: User
Tags naming convention

In general, all lines in the source program are Compiled. however, Sometimes it is desirable to compile a part of the content only if certain conditions are met, that is, to specify the conditions for the compilation of some content, which is "conditional compilation". sometimes, you want to compile a set of statements when a condition is met, and then compile another set of statements when the condition is not met.

The most common form of conditional compilation commands Is:

#ifdef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif

It does this: when the identifier is already defined (typically defined with the # define command), the program Segment 1 is compiled, or the program fragment 2 is Compiled.
The #else part can also be not, namely:
#ifdef
Procedure Section 1
#denif

it is important to use #ifdef and #ifndef in header files to prevent double-defined Errors. as you defined in the header file aaa.h, a Class AAA is as Follows:
Class AAA
  {   
  };
If two times # include "aaa.h" (not necessarily directly, It is also possible that the header file is contained in two different header files), the error occurs because the same class cannot be defined two Times. Change the aaa.h slightly:
#ifndef _aaa_
#define _aaa_
Class AAA
  {   
  };
#endif
you can avoid such problems. because when you have already included this file, the _aaa_ will have a definition, then the #ifndef condition is false and the subsequent class definition will not be executed.

#ifdef和 #endif must be used in Pairs.
theoretically, it can appear anywhere (in header and implementation files)
Usually in order to prevent the header file from being included more than once, it is necessary to use it in the header file:
Such as: #ifndef my_head_h//header file at the beginning, the name is arbitrary, be careful not to conflict with other header files

Header File Declaration
End of #endif//header file

sometimes, in the b.h include "a.h", in "c.h" will include "b.h" and include "a.h", at this time, if not ifndef/endif, will contain two times a.h, resulting in an error.

or the contents of the head file in #ifndef and #endif. Regardless of whether your header file will be referenced by multiple files, you should add This. The general format is this:

#ifndef < identity >
#define < identity >

......
......

#endif

< identity > can theoretically be freely named, but the "identity" of each header file should be Unique. The naming convention for an identity is usually the header file name all uppercase, underlined, and the "." in the file Name. Also becomes underlined, such as: stdio.h

#ifndef _stdio_h_
#define _stdio_h_

......

#endif

2. Define the problem of variables in #ifndef (generally not defined in #ifndef).

#ifndef AAA
#define AAA
...
int i;
...
#endif
There is a variable definition, in the VC link when there is a duplicate definition of the error, and in C successfully compiled.

Conclusion:

(1). when you first use The. CPP file for this header to Generate. obj, int I defines the inside when another one uses This. cpp again [separately] generates. obj, int i is also defined and then two obj by Another. CPP is included in this head, are concatenated together, duplicate definitions will appear.

(2). after changing the source program file extension to. c, The VC compiles the source program according to the C language syntax instead of C + +. In c, if you encounter more than one 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, possible (guessing) when compiling, the C + + language will be the global
The variable is strongly signed by default, so there is a connection Error. C language is based on whether or not the initial strength of the Judgment. Reference

Workaround:

(1). Change the source program file extension to. C.

(2). Recommended Solution:
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;

Note the Problem:

(1). variables are generally not defined in The. H file.

The following is an example of an error that occurs after compilation---------fatal error C1189: #error: WINDOWS. H already in

A situation is essentially a duplicate reference to the Windows.h header file, the solution, if you can not refer to this header file, of course, do not reference, if you have to reference, or refer to other header files caused by indirect reference windows.h, then do not refer to the stdafx.h file, generally can be resolved.

Or you can use the following code:

So use the following code:

#ifdef _windows_
#undef _windows_
#endif

or #ifndef #define #endif

If not, just change the header file location, #include "stdafx.h" before # include <windows.h>

Go to http://www.cnblogs.com/renyuan/archive/2013/05/22/3092362.html

Turn: the role of #ifdef and #endif in C + +

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.