Mail Archives: djgpp/1998/02/09/10:41:53
In article <34DBC8BB DOT 93FEB7A2 AT primenet DOT com> you wrote:
[...]
> everything compiles just fine and peachy, but when it goes to link it
> says multiple definitions of foo and undefined reference to foo. This
> happens repeatedly. I am using structs which I put in the header file.
> These are what give the multiple definition error.
There's an aphorism that describes this very neatly:
Patient: If I move my arm like _this_, it hurts
Doctor: Don't do that, then.
In the case at hand: never put variable *definitions* in a header
file. Only *declarations* should be in there. Definitions belong
in the respective source files. I.e. instead of
--- foo.h: ----
struct something {
/* ... */
} a_variable;
----------------
do this:
--- foo.h: ----
struct something {
/* ... */
}
----------------
extern struct something a_variable;
--- foo.c: ---- (or foo.cc, this is the same for C and C++)
#include "foo.h"
struct something a_variable;
---------------
BTW: maybe you produced this error involuntarily by removing 'typedef'
in modifying your C source to the C++ version?
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -