I work with several files: main.c assembler.c fileHandlers.c handlers.c in addition I have several header files, containing constants, function prototypes etc. In one of them ("datatypes.h") I defined an array of strings:
#ifndef DATATYPES_H
#define DATATYPES_H
const char *OPCODES = {"str1",..., str15}.
#endif
Then, I included this header in all my files (since they all use it at some point).
This is my makefile:
main: main.o assembler.o filesHandler.o handlers.o
gcc -g -Wall -ansi -pedantic main.o assembler.o filesHandler.o handlers.o -o main
main.o: main.c
gcc -g -c -Wall -ansi -pedantic main.c -o main.o
assembler.o: assembler.c
gcc -g -c -Wall -ansi -pedantic assembler.c -o assembler.o
filesHandler.o: filesHandler.c
gcc -g -c -Wall -ansi -pedantic filesHandler.c -o filesHandler.o
handlers.o: handlers.c
gcc -g -c -Wall -ansi -pedantic handlers.c -o handlers.o
When I try to compile, I get the following error:
gcc -g -c -Wall -ansi -pedantic assembler.c -o assembler.o
gcc -g -c -Wall -ansi -pedantic filesHandler.c -o filesHandler.o
filesHandler.c: In function ‘readFile’:
filesHandler.c:14:10: warning: unused variable ‘addressing’ [-Wunused-variable]
char addressing[MAXWORD];
^
gcc -g -Wall -ansi -pedantic main.o assembler.o filesHandler.o handlers.o -o main
assembler.o:(.data+0x0): multiple definition of `OPCODES'
main.o:(.data+0x0): first defined here
filesHandler.o:(.data+0x0): multiple definition of `OPCODES'
main.o:(.data+0x0): first defined here
handlers.o:(.data+0x0): multiple definition of `OPCODES'
main.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
Now, I understand that for some reason, the array is being defined more than once after preprocessing, but I don't know why. I read the Wikipedia article on #include guards, and some more resources out there. They all instruct to do just as I did, but it just doesn't work.
Sorry for loading so much data, but I hope it will prevent unnecessary followups.
Thanks, Elad