Sometimes we want to use some global variables in more than one C program, we think of declaring variables in the header file and #include in the C file. In this case, we have to put the definition of the variable in a C file, but we do not want to do so because it will make the program look bloated. Let's take a look at how Ucos II skillfully solves this problem.
Ucos II has such a piece of code in its header file ucos_ii.h:
#ifdef os_globals
#define os_ext
#else
#define os_ext extern
#endif
Then use OS_EXT macros to declare variables, such as: Os_ext int16u osrdygrp;
Then in one of the C files containing ucos_ii.h #define OS_GLOBALS (see ucos II os_core.c). Note that this sentence must be placed before #include "ucos_ii.h", for reasons you understand (if you do not understand, and see below).
The principle of this technique is understood as long as you understand the nature of the #include "xxx.h" statement. The compiler's handling of the #include "xxx.h" statement in the compilation process is to replace the statement with the entire contents of the xxx.h. In the above example, only the #define OS_GLOBALS is defined in os_core.c, so the Os_ext macros are processed only when the file is compiled, and are treated as extern keywords in other c files.