#ifndef in the file

#ifndef in the header, this is a key thing. Let’s say you have two C files, and both C files include the same header. At compile time, the two C files compile together into a single runnable file, so the problem is a lot of declaration conflicts.

Keep the contents of the header file in #ifndef and #endif. You need to add this regardless of whether your header file is referenced by multiple files. The general format looks like this:

#ifndef < id >

#define < define >

.

.

#endif

< identity > could theoretically be freely named, but the identity of each header file should be unique. The naming rules of the logo are generally all uppercase, underscore before and after the header file name, and put the “. Also becomes an underscore, as in stdio.h

#ifndef _STDIO_H_

#define _STDIO_H_

.

#endif

2. Problems with defining variables in #ifndef (generally not defined in #ifndef).

#ifndef AAA

#define AAA

.

int i;

.

#endif

There’s a variable definition in there

In VC link when I repeated definition of the error, and in C successfully compiled.

Conclusion:

(1). When the first.cpp file that uses this header generates.obj, int I is defined in it. When another.cpp that uses this header generates.obj again, int I is defined and then the two obj are concatenated by another.cpp that also includes this header, There will be repeated definitions.

(2). After the extension of the source program file is changed to.c, VC compiles the source program according to the syntax of C language, rather than C++. In C, if you encounter multiple int I, one of them is automatically considered a definition and the others are declarations.

(3).C language and C++ language connection results are different, possibly (guess) when compiling, C++ language will be global

The variable defaults to strong notation, so the connection failed. C language according to whether initialization strong or weak judgment. (reference)

Solutions:

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

(2) recommended solutions:

H only declares extern int I; Defined in.cpP

#ifndef __X_H__

#define __X_H__

extern int i;

#endif //__X_H__

int i;

Attention issues:

(1). Variables are generally not defined in. H files.

In general, all lines in the source program are compiled. However, sometimes you want to compile part of the content only if certain conditions are met, that is, to specify the conditions for compiling part of the content. This is called “conditional compilation”. Sometimes you want to compile one set of statements when a condition is met and another set when the condition is not.

The most common form of conditional compilation commands is:

# ifdef identifier

Paragraph 1

#else

Paragraph 2

#endif

It compiles segment 1 when an identifier has already been defined (usually with the #define command), and segment 2 otherwise.

The #else part can also be omitted, i.e. :

#ifdef

Paragraph 1

#denif

The “program segment” can be either a statement group or a command line. This conditional compilation can improve the versatility of C source programs. If a C source program runs on different computer systems, and different computers have certain differences. For example, we have a data type that should be represented as long on the Windows platform but as float on other platforms, often requiring necessary modifications to the source, which reduces the program’s versatility. You can compile with the following conditions:

#ifdef WINDOWS

#define MYTYPE long

#else

#define MYTYPE float

#endif

If you compile the 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 appeared before this conditional compilation command:

#define WINDOWS 0

Float is used instead of MYTYPE in precompiled programs. In this way, the source program can be used on different types of computer systems without any modification. Of course, the above is just a simple case, you can set up a glance according to this idea? a href=”javascript:;” onClick=”tagshow(event, ‘%C6%E4%CB%FC’); return false;” > < p style = “max-width: 100%; clear: both; min-height: 1em;

For example, when debugging a program, you often want to output the information you need and then not output it after debugging is complete. You can insert the following conditional compilation sections in the source program:

#ifdef DEBUG

print (“device_open(%p) “, file);

#endif

If it is preceded by the following command line:

#define DEBUG

Output the value of the file pointer while the program is running for debugging analysis. After debugging, simply delete the define command line. One might think you can do this without conditional compilation, that is, adding a bunch of printf statements during debugging and then removing them one by one after debugging. Yes, it can. However, when a large number of printf statements are added during debugging, the workload of modification is very heavy. In conditional compilation, you don’t need to delete the printf statements one by one. You just need to delete the “#define DEBUG” command in front of the printf statements. In this case, all the conditional compilation sections with DEBUG as the identifier make the printf statements useless, that is, the unified control function, like a “switch”.

Sometimes the following form is used:

# # ifndef identifier

Paragraph 1

#else

Paragraph 2

#endif

Only the first line is different from the first form: change “ifdef” to “ifndef”. It compiles segment 1 if the identifier is not defined, and segment 2 otherwise. This form does the opposite of the first.

The above two forms of usage is similar, according to the need to choose one, as convenient and decided.

There is another form where #if is followed by an expression rather than a simple identifier:

# if expression

Paragraph 1

#else

Paragraph 2

#endif

It compiles segment 1 if the specified expression value is true (non-zero), and segment 2 otherwise. Certain conditions can be given in advance to make the program perform different functions under different conditions.

The scope is the current file. Because compilation is done in CPP or C file bits. Take this as an example:

// Normal code

#ifdef _DEBUG

TRACE(“Some infomation”);

#else

//Now is release version,so do nothing

#endif

// Normal code

During compilation, all precompilation processes (such as macros) are expanded before compilation. Therefore, in Debug mode, the code during compilation is:

// Normal code

TRACE(“Some infomation”);

// Normal code

The code for Release mode is:

// Normal code

// Normal code


Original address: original address of this article