multiple definition of `fVar'

Discussion in 'C Programming' started by Carramba, Apr 14, 2005.

  1. Carramba

    Carramba Guest

    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


    ______________________________
     
    Carramba, Apr 14, 2005
    #1
    1. Advertising

  2. 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
     
    Christian Kandeler, Apr 14, 2005
    #2
    1. Advertising

  3. Carramba

    Guest

    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.
     
    , Apr 14, 2005
    #3
  4. 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
     
    Krishanu Debnath, Apr 14, 2005
    #4
  5. 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
     
    Christian Kandeler, Apr 14, 2005
    #5
  6. Christian Kandeler <_invalid> writes:
    > 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) <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.
     
    Keith Thompson, Apr 14, 2005
    #6
  7. Carramba

    pete Guest

    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
     
    pete, Apr 14, 2005
    #7
  8. pete <> 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) <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.
     
    Keith Thompson, Apr 14, 2005
    #8
  9. Carramba

    pete Guest

    Keith Thompson wrote:
    >
    > pete <> 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
     
    pete, Apr 15, 2005
    #9
  10. Carramba

    S.Tobias Guest

    Keith Thompson <> wrote:
    > pete <> 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 LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Apr 15, 2005
    #10
  11. Carramba

    pete Guest

    S.Tobias wrote:
    >
    > Keith Thompson <> wrote:
    > > pete <> 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
     
    pete, Apr 15, 2005
    #11
  12. Carramba

    S.Tobias Guest

    pete <> 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 LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Apr 15, 2005
    #12
  13. Carramba

    pete Guest

    S.Tobias wrote:
    >
    > pete <> wrote:
    > > S.Tobias wrote:

    >
    > > > > >> Type definitions

    > >
    > > > Do you now


    .... or have you ever

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


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

    --
    pete
     
    pete, Apr 15, 2005
    #13
  14. Carramba

    S.Tobias Guest

    pete <> wrote:
    > S.Tobias wrote:
    > > pete <> wrote:
    > > > S.Tobias wrote:

    > >
    > > > > > >> Type definitions
    > > >
    > > > > Do you now


    > ... or have you ever


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


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

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Apr 15, 2005
    #14
  15. Carramba

    pete Guest

    S.Tobias wrote:
    >
    > pete <> wrote:
    > > S.Tobias wrote:
    > > > pete <> wrote:
    > > > > S.Tobias wrote:
    > > >
    > > > > > > >> Type definitions
    > > > >
    > > > > > Do you now

    >
    > > ... or have you ever

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

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

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


    --
    pete
     
    pete, Apr 15, 2005
    #15
  16. Carramba

    S.Tobias Guest

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

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


    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Apr 15, 2005
    #16
  17. Carramba

    pete Guest

    S.Tobias wrote:
    > pete <> wrote:
    > > S.Tobias wrote:
    > > > pete <> 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?selm=

    --
    pete
     
    pete, Apr 15, 2005
    #17
  18. Carramba

    S.Tobias Guest

    pete <> wrote:
    > S.Tobias wrote:
    > > pete <> wrote:
    > > > S.Tobias wrote:
    > > > > pete <> 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?selm=


    I don't quite understand what you're trying to tell me. I'm
    splitting hairs? 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.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Apr 15, 2005
    #18
  19. Carramba

    S.Tobias Guest

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

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Apr 15, 2005
    #19
  20. Carramba

    pete Guest

    S.Tobias wrote:
    >
    > pete <> wrote:
    > > S.Tobias wrote:
    > > > pete <> wrote:
    > > > > S.Tobias wrote:
    > > > > > pete <> 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?selm=

    >
    > 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
     
    pete, Apr 16, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    603
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    437
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    458
    Jon Slaughter
    Oct 26, 2005
  4. Replies:
    11
    Views:
    1,251
    Ian Collins
    Aug 5, 2006
  5. Pierre Yves
    Replies:
    2
    Views:
    499
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page