Turn from: http://blog.csdn.net/fengyu09/article/details/9929603
Declaring global variables and creating extern in header files
Problem:
Is there a simple way to define my variables in the header file and create an extern definition for them?
Reply:
Yes. Although this is not required, it is easy to implement using the correct macro and a header file.
Typically, you declare a variable in the C file and create an extern definition in the header file. However, if you want, the following tips are used to declare variables in the same head file and to define them in an extern definition.
first , create a header file as follows (such as VARS.H):
[CPP] View Plain Copy/*---------------------------------------------------------------------------- VARS.H NOTE: Use the #define  VAR_DECLS 1 statement before including the VARS.H header file declaration and initialization global variables. just contains it, and does not define VAR_DECLS to refer to variables. ----------------------------------------------------------------------------*/ #ifndef var_defs // guarantee file is only included once #define VAR_DEFS 1 /*---------------------------------------------- SET Variable declaration macros ---------- ------------------------------------*/ #ifndef VAR_DECLS # define _decl extern # define _init (x) #else # define _decl # define _init (x) = x #endif /*------------------------- --------------------- Declare variables as follows: _decl [standard variable declaration] _init (x); x is the value of the initialization variable. If the variable has no initialized value, you can declare the following &NBSP _decl [standard variable declaration]; ----------------------------------------------*/ _decl int var_ A _init (m); _decl int var_b; _decl int var_c _init (27); #endif /*---------------------------------------------------------------------------- ----------------------------------------------------------------------------*/
The VARS.H declares 3 global variables:
int var_a = 100;
int var_b;
int var_c = 27;
2nd step , in the same source file, contains the following #define before the include Vars.h:
[CPP] view plain copy #define VAR_DECLS #include "vars.h"
#define VAR_DECLS Raises the vars.h contains the actual declaration of the file and initializes the global variable.
finally , simply include the vars.h header file in all other source files (using these variables):
[CPP] view plain copy #include "Vars.h"
Make sure VAR_DECLS is not defined in other files, otherwise your variable will be declared 2 times.
From the domestic UC/OS-II 2.0 Word Document description (Lu Jinhua):
Global variables
Here's how to define global variables. As we all know, global variables should be variables that get memory allocations and that can be invoked by other modules through the extern keyword in C language. Therefore, must be in. C and. H-File definition. This repetitive definition can easily lead to errors. The methods discussed below only need to be defined once in the header file. Although a bit not easy to understand, but the user once mastered, use is very flexible. The definition in table 1.2 appears in the. h header file that defines all global variables.
The program list L 1.2 defines global macros.
[CPP] view plain copy #ifdef xxx_globals #define Xxx_ext