473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

multiple definition of `fVar'

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
14 2509
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jochen Zeischka | last post by:
Hi everybody! I have a question concerning code organisation. Suppose I have the following header file: #ifndef SOME_NAME #define SOME_NAME namespace N { void F()
11
by: Georg Teichtmeister | last post by:
Hello! We are developing a math - library for realtime applications and want to use some given mathlibraries as base(ipp, MTL, .. ). Our library is a wrapper for those and you should be able to...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
9
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings....
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
11
by: lars.uffmann | last post by:
Easily described problem: Using g++ version 3.3.5 under suse 9.3, bla.h: ----------- #ifndef myTEST #define myTEST ZFSInt test; #endif
3
by: Student | last post by:
Hi all, While compiling a program I had this message : tools.o(.data+0x0): multiple definition of `VAR_1' main.o(.data+0x0): first defined here tools.o(.data+0x4): multiple definition of...
3
by: Jens Müller | last post by:
I have a file here with several enums: #ifndef PLANARSEP_OPTIMIZE_H #define PLANARSEP_OPTIMIZE_H enum fund_cycle_behavior_t {PASS_MODE_FIRST, PASS_MODE_BEST, PASS_MODE_ALL};
10
by: zfareed | last post by:
Similar problem to the previous post. I have created a project with about 7 files including 3 header files and 3 implementation files. I am getting a multiple definition error when compiling for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.