Here's my header file.
#ifndef P6_H
#define P6_H
#include <stdio.h>
void FoundationC();
void StructureC();
void PlumbingC();
void ElectricC();
void HVACC();
void SheatingC();
extern int DAYS;
#endif
I'm using a makefile to do all compilation. It's able compile the individual .o files file but when it tries turn those into a single executable it says that there multiple definitions of the variable DAYS even though that is extern and declared and initialized in each individually. I got this to work before but can't figure out why it's not working now. Oh and here's my makefile code
all:
gcc -c P6.c
gcc -c foundations.c
gcc -c structure.c
gcc -c plumbing.c
gcc -c electric.c
gcc -c hvac.c
gcc -c sheating.c
gcc P6.h P6.o foundations.o structure.o plumbing.o electric.o hvac.o sheating.o -o P6
I realize P6.h probably doesn't have to be include in the command but the include guards should make it not matter no?
Also I'm sorry if this question is a dupe but I did previously look for answers and this issue is driving me crazy on a personal level despite the fact that's it's for school.
Here are the errors I get.
gcc -c P6.c
gcc -c foundations.c
gcc -c structure.c
gcc -c plumbing.c
gcc -c electric.c
gcc -c hvac.c
gcc -c sheating.c
gcc P6.h P6.o foundations.o structure.o plumbing.o electric.o hvac.o sheating.o -o P6
structure.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
plumbing.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
electric.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
hvac.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
sheating.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
To help you understand, the following is an extension of @FUZxxl's answer, which is correct. If you have the following in your compilation unit (a compilation unit is the .c source file plus all included .h files):
extern int DAYS;
...
int DAYS = 1;
then the second declaration of DAYS
overrides the first declaration which stated it is an extern
. So, the variable is now no longer an extern
and if you do this in more than one source file you now consequently have multiple definitions and the linker complains.
If you must initialize DAYS
, then you can do that in one place, preferably in the main file.