We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I keep getting a linker error suggesting that a few variables (and some functions) have been declared across multiple files. I've triple checked the files (they're not large) and the erring functions/variables have NOT been declared more than once.
Interestingly, it seems to occur between the file that contains the main function and file A for some, and main and B for some others. Which means that the main file is common. But, the main file does not have these declarations.
Can multiple *definitions* of the same C files, or including the same C files across different #include appearances (in different files) cause the problem?
Any help will be appreciated.
Most likely, you have definitions in a header (or headers).
Obviously, if a header file containing definitions is #included in multiple .c files, then you will get multiple definitions!
Your headers should contain only extern declarations...
Thanks, Andy, for your response. As it turned out, my files A.c and B.c were included in the main file AND added separately as part of the project. I did not know that this was not allowed. Removing the included files and allowing them to show up only as dependencies fixed the problem sharpish i.e. the code compiles and creates a Hex file too.
Now the issue has migrated - the Hex file does not work on the microcontroller (LPC2119). This is strange because I have a Hex file generated from the same program left behind by last year's student, and HIS hex file works when Flashed (Flash Magic / LPC2000 both) but not mine - same source code. The target is correctly set, as is the crystal and everything else is default in Keil. What could I be doing wrong?
I'd be grateful if you could give me some ideas.
"files A.c and B.c were included in the main file AND added separately as part of the project. I did not know that this was not allowed."
It is allowed; but if you do it, then you have to ensure that it is done right - in particular, that it doesn't cause multiple definitions.
However, just because it can be done doesn't mean that it's a good idea!
In general, you do not #include .c files - you compile them separately, and let the Linker join the pieces together...