By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,843 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,843 IT Pros & Developers. It's quick & easy.

multiple definition of `fVar'

P: n/a
hi!
I have program with several funktion witch are in separete files,
I have one include file were I have definet some variables and initiated
'const double fVar=0.874532;'
this files is includet in all other files containing funktions,
when I compile I get this error multiple definition of `fVar'
why id that? I have only defined it one in include file?
--
thanx in advance
______________________________
Nov 14 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Carramba wrote:
I have one include file were I have definet some variables
Bad idea.
and initiated 'const double fVar=0.874532;'
this files is includet in all other files
Very bad idea.
containing funktions,
when I compile I get this error multiple definition of `fVar'
why id that? I have only defined it one in include file?


For the compiler, including a header file is the same as writing its
contents directly into the source file. Hence you have defined your
variable not once, but n times, where n is the number of source files your
header file gets included in.
_Never_ define variables in header files. Only _declare_ them there:
extern const double fVar;
Then, choose an appropriate source file to do the definition and
initialization.
Christian
Nov 14 '05 #2

P: n/a
Even though your variable is defined in only one header say 'a.h', the
header might get included multiple times (via another header which
includes 'a.h'). To avoid this situation, write some preprocessing
definitions at beginning of the header like:

#ifndef a_h
<your variable declarations>
#endif
#define a_h

This will solve the problem of multiple definitions since the
declarations/definitions are included only if they are not included
earlier.

Nov 14 '05 #3

P: n/a
ni***********@yahoo.com wrote:
Even though your variable is defined in only one header say 'a.h', the
header might get included multiple times (via another header which
includes 'a.h'). To avoid this situation, write some preprocessing
definitions at beginning of the header like:

#ifndef a_h
<your variable declarations>
#endif
#define a_h

This will solve the problem of multiple definitions since the
declarations/definitions are included only if they are not included
earlier.


No, It does not solve the OP's problem. Christian already suggested the
right solution.

Krishanu
Nov 14 '05 #4

P: n/a
ni***********@yahoo.com wrote:
Even though your variable is defined in only one header say 'a.h', the
header might get included multiple times (via another header which
includes 'a.h'). To avoid this situation, write some preprocessing
definitions at beginning of the header like:

#ifndef a_h
<your variable declarations>
#endif
#define a_h

This will solve the problem of multiple definitions


No, it will not. Include guards prevent including the same header twice in a
particular source file, which wasn't the OP's problem.
Christian
Nov 14 '05 #5

P: n/a
Christian Kandeler <ch****************@hob.de_invalid> writes:
ni***********@yahoo.com wrote:
Even though your variable is defined in only one header say 'a.h', the
header might get included multiple times (via another header which
includes 'a.h'). To avoid this situation, write some preprocessing
definitions at beginning of the header like:

#ifndef a_h
<your variable declarations>
#endif
#define a_h

This will solve the problem of multiple definitions


No, it will not. Include guards prevent including the same header twice in a
particular source file, which wasn't the OP's problem.


Right.

Type definitions and external function and variable declarations (not
definitions) can be defined in a header, protected by include guards,
because they need to occur exactly once in each translation unit.
They exist for the benefit of the compiler.

Variable and function definitions need to be defined in non-header
source files (*.c files) because they need to occur exactly once in
each program, which may consist of multiple translation units. They
exist for the benefit of the linker, so include guards to not suffice
to restrict them to a single occurrence.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

P: n/a
Keith Thompson wrote:
Type definitions
Types are declared.
Only objects and functions are defined.
and external function and variable declarations (not
definitions) can be defined in a header, protected by include guards,
because they need to occur exactly once in each translation unit.


/* BEGIN c_story.c */

int puts(const char *);
int puts(const char *);

int main(void)
{
puts(
"\nWhat do you mean by\n"
"\"they need to occur exactly once in each translation unit\"?"
);
return 0;
}

int puts(const char *);

/* END c_story.c */
--
pete
Nov 14 '05 #7

P: n/a
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
Type definitions


Types are declared.
Only objects and functions are defined.


Ok. (That makes things more consistent.)
and external function and variable declarations (not
definitions) can be defined in a header, protected by include guards,
because they need to occur exactly once in each translation unit.

[counterexample snipped]

Another good point. External declarations may be declared multiple
times. (But function and object definitions, for example, may not.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

P: n/a
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
Type definitions


Types are declared.
Only objects and functions are defined.


Ok. (That makes things more consistent.)
and external function and variable declarations (not
definitions) can be defined in a header,
protected by include guards,
because they need to occur exactly once in each translation unit.

[counterexample snipped]

Another good point. External declarations may be declared multiple
times. (But function and object definitions, for example, may not.)


Now, what do you think what the include guards are really for?

--
pete
Nov 14 '05 #9

P: n/a
Keith Thompson <ks***@mib.org> wrote:
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
Type definitions
Types are declared.
Only objects and functions are defined.

Ok. (That makes things more consistent.)


Well, actually Clause 6.7.7 is entitled... "Type definitions".

6.7p5 defines a /definition/ to be also a declaration of an enumeration
constant and of a typedef name (beside functions and objects).

Do you now the reason for this (strange) terminology?

[Note: the Standard also defines the term /external definition/.]

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #10

P: n/a
S.Tobias wrote:

Keith Thompson <ks***@mib.org> wrote:
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:

> Type definitions

Types are declared.
Only objects and functions are defined.
Ok. (That makes things more consistent.)


Well, actually Clause 6.7.7 is entitled... "Type definitions".

6.7p5 defines a /definition/ to be also a
declaration of an enumeration
constant and of a typedef name (beside functions and objects).


Thank you.
Do you now the reason for this (strange) terminology?
Because they didn't think to call it "typedec" instead?
[Note: the Standard also defines the term /external definition/.]


--
pete
Nov 14 '05 #11

P: n/a
pete <pf*****@mindspring.com> wrote:
S.Tobias wrote:
>> Type definitions

Do you now the reason for this (strange) terminology?

Because they didn't think to call it "typedec" instead?


So if they thought the keyword to be "typefoo", we would
call it "fooition"?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #12

P: n/a
S.Tobias wrote:

pete <pf*****@mindspring.com> wrote:
S.Tobias wrote:
pete <pf*****@mindspring.com> wrote:
> S.Tobias wrote:
> > pete <pf*****@mindspring.com> wrote: > > I just have to remember that there's
> > > three kinds of things that can be defined:
> > > 1 objects
> > > 2 functions
> > > 3 types
> > 4 enumeration constants :-)

> I don't think so for enumeration constants. # 5 A declaration specifies the interpretation and
# attributes of a set of identifiers. A /definition/ of an
# identifier is a declaration for that identifier that:
# -- for an object, causes storage to be reserved for that object;
# -- for a function, includes the function body;98)
# -- for an enumeration constant or typedef name, is the
# (only) declaration of the identifier.
Well, that settles it then.
Thank you very much.

I would like to learn why.

http://groups.google.co.uk/groups?se...mindspring.com


I don't quite understand what you're trying to tell me.


I don't always expect to be able to understand the committee.

I think I see what you mean about what the standard
says about external definitions.
An external typedef is an external declaration, and a definition,
but it is not an external definition.

--
pete
Nov 14 '05 #13

P: n/a
S.Tobias wrote:

S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
Well, I would like to find out whether there is
something I don't know yet; I hope there's more to it than just
aesthetic choice of words.


BTW, [OT], I've just checked C++ Standard defines the term
"definition" in a different manner (rather by exclusion), and in
particular, enum constant declaration is a "definition", but typedef
declaration is a "declaration".


The standard also refers to "macro definition".

--
pete
Nov 14 '05 #14

P: n/a
pete <pf*****@mindspring.com> wrote:
S.Tobias wrote:

S.Tobias <si***@famous.bedbug.pals.invalid> wrote:
Well, I would like to find out whether there is
something I don't know yet; I hope there's more to it than just
aesthetic choice of words.
BTW, [OT], I've just checked C++ Standard defines the term
"definition" in a different manner (rather by exclusion), and in
particular, enum constant declaration is a "definition", but typedef
declaration is a "declaration".

The standard also refers to "macro definition".


:-) This is yet another species of "definition". The kind we are
talking about is a "definition" which is a "declaration".

I think I'll pursue this topic in c.s.c. soon.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.