Customary practice is to declare an object (or function) in a header file and define it in one source file. For example, given an object named foo
defined as MyType foo = 0;
in a file named bar.c
, the header file bar.h
would declare MyType foo;
. Then each source file that needed foo
would use #include "bar.h"
to get its declaration.
The author of the code you are asking about apparently decided to put the definition and declaration together. Their header file intdspmng.h
(presumed from the name of the include guard) can be included in source files and will declare identifiers normally. But, when they include it in main.c
or whatever they have designated as their main file, they first define _MAIN_
, perhaps with #define _MAIN_
or #define _MAIN_ 1
. Then, when they include intdspmng.h
, it defines the identifiers instead of just declaring them.
In other words, that person may have thought “I want to keep the declarations and definitions of these things together, because it is easier to edit them that way and to see the association between the definition and the declaration. So I will write code that I can put in the header file but that only defines the things in my main file.”
That might not be entirely unreasonable. However, they have made a mistake. One purpose the declarations serve is providing a check that they match the definitions. If in the source file where identifiers are defined we also include the header that declares them, then the compiler sees the declarations and the definitions together, and it will report warnings or errors if they conflict, as can happen when a typographical error is made or a type is changed in one place but not another.
This can be remedied by always including the declarations, while the definitions remain included only if requested by the definition of _MAIN_
:
extern dspList_t *gDspmngTab;
extern int nb_config_dspmng;
extern PARAM_COM_WEBSRV ParamCom;
#ifdef _MAIN_
dspList_t *gDspmngTab = NULL;
int nb_config_dspmng = 0;
PARAM_COM_WEBSRV ParamCom;
#endif
While that remedies this deficiency, I still would not say that I recommend this approach.
Another issue is that identifiers beginning with an underscore followed by an uppercase letter or another underscore are reserved, according to the C standard. They should be avoided in general program code, although compilers and “system” libraries may use them. So _MAIN_
should be changed to something else, such as DefineMainObjects
.