#ifndef the use of #define#endif

Source: Internet
Author: User
Tags naming convention

#ifndef the use of #define#endif

#ifndef in the file

The #ifndef of the first piece, this is a very important thing. For example, you have two C files, and these two C files include the same header file. At compile time, these two C files are compiled into a running file together, so the problem comes with a lot of conflicting statements.

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's a variable definition inside.
When I link in VC, I repeat the error of definition, and compile successfully in C.

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.

--------------------------------------------------------------------------------------------------------------- ----------------------------

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

The "program segment" Here can be a statement group, or it can be a command line. This kind of conditional compilation can improve the generality of C source program. If a C source program is running on a system with different computer systems, there are some differences between the different computers. For example, we have a data type that should be represented in the Windows platform using the long type, whereas float is used on other platforms, which often requires the necessary modifications to the source program, which reduces the generality of the program. Can be compiled with the following conditions:
#ifdef WINDOWS
#define MYTYPE Long
#else
#define MYTYPE Float
#endif

If you are compiling a program on Windows, you can add it at the beginning of the program
#define WINDOWS

This compiles the following command line:
#define MYTYPE Long

If the following command line appears before this set of conditional compilation commands:
#define WINDOWS 0

The MyType in the pre-compiled program are replaced with float. In this way, the source program can be used for different types of computer systems without having to make any modifications. Of course, the above introduction is only a simple situation, can be based on this idea to design other conditional compilation.
For example, when you are debugging a program, you often want to output some of the required information, and no longer output it when debugging is complete. You can insert the following conditional compilation segments in the source program:
#ifdef DEBUG
Print ("Device_open (%p)", file);
#endif

If you have the following command line in front of it:
#define DEBUG

The value of the file pointer is output when the program runs to debug the analysis. Simply remove the Define command line after debugging. Some people may feel that without conditional compilation can also achieve this purpose, that is, in debugging, add a batch of printf statements, after debugging one after the printf statement deleted. Indeed, this is possible. However, when you add more than one printf statement when debugging, the amount of work that is modified is significant. With conditional compilation, you do not have to one by one to revise the printf statement, just delete the previous "#define DEBUG" command, then all the conditional compilation with DEBUG identifier to make the printf statement does not work, that is, the role of unified control, like a "switch".
It is sometimes used in the following form:
#ifndef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif

Just the first line is different from the first: Change "ifdef" to "ifndef". Its function is: If the identifier is not defined, compile the program segment 1, or compile the program segment 2. This form is contrary to the effect of the first form.
The above two forms of use are similar, depending on the need to choose one, depending on the convenience.
There is also a form of an expression that is followed by # If, not a simple identifier:
#if an expression
Procedure Section 1
#else
Procedure Section 2
#endif

It does this by compiling program segment 1 when the specified expression value is true (not 0), or by compiling program segment 2. A certain condition can be given beforehand to enable the program to perform different functions under different conditions.

--------------------------------------------------------------------------------------------------------------- ------------------------

The scope is the current file. Because the compilation is in CPP or c file bit units. Also take this as an example:

Normal code
#ifdef _DEBUG
TRACE ("Some infomation");
#else
Now was release version,so do nothing
#endif
Normal code

Compile-time is the first to compile all the precompiled processing (such as macros), so in debug mode, compile-time code is:
Normal code
TRACE ("Some infomation");
Normal code

The code in release mode is:
Normal code
Normal code

#ifndef the use of #define#endif

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.