A source file farm.c has an associated header file farm.h: /* file farm.h */ int
ID: 3622665 • Letter: A
Question
A source file farm.c has an associated header file farm.h:
/* file farm.h */
int cluck;
int arf(int, int);
void moo();
double meow(double);
where cluck is a variable to be used in several source files and arf, moo, and meow are functions defined in farm.c farm.h is #included into files farm.c, city.c, village.c, and factory.c which all form parts of one program. These files will compile without error but will produce a linker error. What is the mistake and what problem did it cause for the linker?
Explanation / Answer
int cluck is included in farm.c city.c village.c and factory.c as farm.h is included in all of them. So when you compile all these individual files there won't be any errors. However when the linker tries to link all the files it generates an error because the program ( contains farm.c village city and factory under 1 file) has 4 variables of the same name (cluck). Therefore it gives u an error. That is why the number 1 thing to remember when u are making your own header files is that never include any variable definitions in it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.