is a NO-NO! My colleague asked me why – so here is the answer.
If you do something like this:
- Declare a variable, say ‘int glob_x’ in ‘x.h’
- Include ‘x.h’ in ‘a.c’ and ‘b.c’
- Try to link a.o and b.o into one executable, and you will get a linker error, duplicate symbol.
Basically, the header file is included in both the files, and the variable glob_x is now global in *both* the c files, which leads to a clash when you try to link them together.
The correct way of doing this is:
Make glob_x extern in x.h, and declare the variable in only one of your .c files. Now you can use the variable in any .c files that include x.h, and there will be no linker error.