On Wed, 15 Feb 2006 10:24:38 -0800, "osmium" <r1********@comcast.net>
wrote:
"gamehack" writes:
Let me describe the problem. I've a header file util.h which contains
the following:
#ifndef UTIL_H_
#define UTIL_H_
const char DEF_TITLE[] = "My Title";
Header guards do not protect against multiple definitions. Each program
segment that includes this header gets exactly one definition for DEF_TITLE.
More specifically, each 'translation unit' = source file + includes.
There should only be *type* definitions or variable *declarations* in a
header.
Change it to external (q.v.) which will demote it to a declaration. Put a
definition in one of the files where it makes sense.
Just adding 'extern' (not 'external', and not replacing anything
already there) to a declaration with an initializer doesn't help; it's
still a definition. You need to declare with extern and no
initializer, and then (as you say) define in one t.u.
It may be a problem (or not) that in that scheme the declaration
extern const char DEF_TITLE [] ;
has unknown bound/size, whereas with the initializer it has a known
size from the initial value even though not explicitly written. If
this matters you can duplicate the value in each t.u. as
static const char DEF_TITLE [] = "Value";
(It is even possible, and certainly permitted though not required,
that the implementation might be clever enough to merge these.)
- David.Thompson1 at worldnet.att.net