multiple definition of `fVar'

C

Carramba

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?
 
C

Christian Kandeler

Carramba said:
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
 
N

nitin_prasant

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.
 
K

Krishanu Debnath

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
 
C

Christian Kandeler

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
 
K

Keith Thompson

Christian Kandeler said:
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.
 
P

pete

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 */
 
K

Keith Thompson

pete said:
Types are declared.
Only objects and functions are defined.

Ok. (That makes things more consistent.)
[counterexample snipped]

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

pete

Keith said:
pete said:
Types are declared.
Only objects and functions are defined.

Ok. (That makes things more consistent.)
[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?
 
S

S.Tobias

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/.]
 
P

pete

S.Tobias said:
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/.]
 
P

pete

.... or have you ever
So if they thought the keyword to be "typefoo", we would
call it "fooition"?

I just have to remember that there's
three kinds of things that can be defined:
1 objects
2 functions
3 types
 
S

S.Tobias

... or have you ever
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 didn't mean to make any jokes, my question was genuine and
I'll ask it in c.s.c. if I don't get an answer here (which
I was going to do anyway).

Until I discovered this in the Standard, I also thought only
functions and objects could be defined, all the rest were
merely (pure) declarations. The distinction seemed natural
to me: declaration is compile-time device, and a definition
causes something to appear in the run-time (well, this is
not entirely true, but... ya know what I mean).

Now, the Standard explicitly included declarations of typedef names
and enumeration constants and I think there must have been some
reason for this. (I don't think the reason was that someone already
had invented "typedef" keyword; besides, there's no "def" in "enum".)

What is the notion that the term /definition/ conveys? Why aren't
other declarations (eg. struct declaration with a defining body)
encompassed by the "definition"?
 
P

pete

S.Tobias said:
4 enumeration constants :)

I don't think so for enumeration constants.
By "be defined",
I meant have a declaration which is also a definition.

These are the only occurances of "define" that I can find
in relation to enumeration constants in N869.

" An enumerator with = defines its
enumeration constant as the value of the constant
expression. If the first enumerator has no =, the value of
its enumeration constant is 0. Each subsequent enumerator
with no = defines its enumeration constant as the value of
the constant expression obtained by adding 1 to the value of
the previous enumeration constant."

I think they're using the word "define" in it's
standard English meaning, rather than as what
constitutes a "definition" in a C program.

There's no words that say that
the first enumeration constant is defined
if it doesn't have an =, only subsequent members.

Consider:
enum numbers{ONE, TWO};
ONE is defined as zero in the same way that TWO is defined as one,
but there's no words in the standard saying that ONE is "defined"
in any way, except in the standard English way.
I didn't mean to make any jokes, my question was genuine and
I'll ask it in c.s.c. if I don't get an answer here (which
I was going to do anyway).

Until I discovered this in the Standard, I also thought only
functions and objects could be defined, all the rest were
merely (pure) declarations. The distinction seemed natural
to me: declaration is compile-time device, and a definition
causes something to appear in the run-time (well, this is
not entirely true, but... ya know what I mean).

I *do* know what you mean.
 
S

S.Tobias

I don't think so for enumeration constants.
By "be defined",
I meant have a declaration which is also a definition.
These are the only occurances of "define" that I can find
in relation to enumeration constants in N869.

[snip quote from 6.7.2.1#3]
I think they're using the word "define" in it's
standard English meaning, rather than as what
constitutes a "definition" in a C program.

Yes, I agree, the words "define", "definition" are used mostly
in colloquial way, but I have been only referring to 6.7p5:

# 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.

The word "definition" is in italics in the Standard, hence
introduces the term "definition".

[snip]
Consider:
enum numbers{ONE, TWO};
ONE is defined as zero in the same way that TWO is defined as one,
but there's no words in the standard saying that ONE is "defined"
in any way, except in the standard English way.

IIUC the above excerpt, the Standard designates those
declarations (ONE, TWO) also "definitions". I would like
to learn why.
 
P

pete

# 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/[email protected]
 
S

S.Tobias

S.Tobias said:
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".
 
P

pete

S.Tobias said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top