Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > multiple definition of `fVar'

Reply
Thread Tools

multiple definition of `fVar'

 
 
Carramba
Guest
Posts: n/a
 
      04-14-2005
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


______________________________
 
Reply With Quote
 
 
 
 
Christian Kandeler
Guest
Posts: n/a
 
      04-14-2005
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
 
Reply With Quote
 
 
 
 
nitin_prasant@yahoo.com
Guest
Posts: n/a
 
      04-14-2005
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.

 
Reply With Quote
 
Krishanu Debnath
Guest
Posts: n/a
 
      04-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Christian Kandeler
Guest
Posts: n/a
 
      04-14-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-14-2005
Christian Kandeler <(E-Mail Removed)_invalid> writes:
> (E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-14-2005
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-14-2005
pete <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-14-2005
Keith Thompson wrote:
>
> pete <(E-Mail Removed)> 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
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      04-14-2005
Keith Thompson <(E-Mail Removed)> wrote:
> pete <(E-Mail Removed)> 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 (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
How to avoid multiple definition of a variable by multiple inclusion of a header file lars.uffmann@rwth-aachen.de C++ 11 08-05-2006 10:24 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments