I am trying out standard method of guarding a .h (example:checkpgm.h) file with guard variables as follows
#ifndef _CHECKPGM
#warning CHECKPGM value is 0
#define _CHECKPGM 1
char a;
#endif
This code is included in two files checkmain.c and checksub.c
When these two files are linked I get the error message
Building target: checkpgm
Invoking: GCC C Linker
gcc -o "checkpgm" ./src/checkmain.o ./src/checksub.o
/usr/bin/ld: ./src/checksub.o:/home/nagesh/eclipse-workspace/multi/checkpgm/Debug/../src/checkpgm.h:18: multiple definition of `a'; ./src/checkmain.o:/home/nagesh/eclipse-workspace/multi/checkpgm/Debug/../src/checkpgm.h:18: first defined here
I am unable to understand how this is happening in-spite of guarding.
I tried
- Changing #define _CHKPGM to #define _CHKPGM 1 (explicit value assignment)
- Similarly, #if _CHKPGM == 0
- Checking with #if defined
- Adding _CHECKPGM with -D flag (Undefined)
- Somehow standard libraries are not having this problem (stdio.h, stdlib.h); So copied their style _BEGIN_DECLS...
- By warning messages, it is clear that _CHECKPGM gets assigned in its first inclusion. However This value will be lost when compilation of second program begins and hence guard fails. But unable to figure out why?.
Can you folks please help us out?
Thanking in advance...
RaviJ
checkpgm.h is included in