www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/09/10:41:53

Date: Mon, 9 Feb 1998 16:41:24 +0100
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Message-Id: <199802091541.QAA31992@acp3bf.physik.rwth-aachen.de>
To: libolt AT primenet DOT com (Mike McLean)
Cc: djgpp AT delorie DOT com
Subject: Re: multiple definitons
Newsgroups: comp.os.msdos.djgpp
Organization: RWTH Aachen, III. physikalisches Institut B

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019