I saw some similar questions, but none of them mentioned #ifndef HEADER_H.
I have a header file and 2 C files:
constants.h
main.c
mylib.c
In constants.h :
#ifndef CONSTANTS_H
#define CONSTANTS_H
const int NUM_OF_ITEMS = 22;
#endif
In mylib.c :
#include "constants.h"
... code ...
In main.c :
#include "constants.h"
int main() {
... code ...
}
When I compile the command: gcc main.c mylib.c -o mainI get the following error:
/tmp/ccl55fv3.o:(.rodata+0x0): multiple definition of `NUM_OF_ITEMS'
/tmp/ccyZhu6F.o:(.rodata+0x0): first defined here
collect2: ld returned 1 exit status
- I mentioned
#ifndef, so why is this happening? - Is there anything other than splitting on
constants.hfor an announcement and constants.cfor an appointment?
source
share