Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote