Error compiling multiple file projects:

Discussion in 'C++' started by Gaijinco, Apr 13, 2007.

  1. Gaijinco

    Gaijinco Guest

    I'm having a weird error compiling a multiple file project:

    I have three files:

    tortuga.h where I have declared 5 global variables and prototypes for
    some functions.
    tortuga.cpp where I implement all of the functions of tortuga.h
    main.cpp where I use the functions implemented in tortuga.cpp

    I create the objetc file without problem. But when I try to compile
    "main.cpp" it says:

    "multiple declaration of..." for each one of the global variables.

    What am I doing wrong?

    Thanks.
    Gaijinco, Apr 13, 2007
    #1
    1. Advertising

  2. Gaijinco wrote:
    > I'm having a weird error compiling a multiple file project:
    >
    > I have three files:
    >
    > tortuga.h where I have declared 5 global variables and prototypes for
    > some functions.
    > tortuga.cpp where I implement all of the functions of tortuga.h
    > main.cpp where I use the functions implemented in tortuga.cpp
    >
    > I create the objetc file without problem. But when I try to compile
    > "main.cpp" it says:
    >
    > "multiple declaration of..." for each one of the global variables.
    >
    > What am I doing wrong?


    Run just the pre-processor over main.cpp and see where they are multiply
    defined. It appears that you're including the header twice somehow.
    Gianni Mariani, Apr 13, 2007
    #2
    1. Advertising

  3. Gaijinco

    Howard Guest

    "Gaijinco" <> wrote in message
    news:...
    > I'm having a weird error compiling a multiple file project:
    >
    > I have three files:
    >
    > tortuga.h where I have declared 5 global variables and prototypes for
    > some functions.
    > tortuga.cpp where I implement all of the functions of tortuga.h
    > main.cpp where I use the functions implemented in tortuga.cpp
    >
    > I create the objetc file without problem. But when I try to compile
    > "main.cpp" it says:
    >
    > "multiple declaration of..." for each one of the global variables.
    >
    > What am I doing wrong?


    Don't declare global variables in header files. Instead, if you want them
    to be visible from multiple source files, then move the declarations to a
    source file, and then declare them as "extern" in the header file.

    Better yet, don't use global variables at all. But that's another
    question...

    -Howard
    Howard, Apr 13, 2007
    #3
  4. Gaijinco

    Gaijinco Guest

    It appears that you're including the header twice somehow.

    Should be enough to use

    #ifndef FILE_H
    #define FILE_H

    #endif

    to stop including the header more than once?
    Gaijinco, Apr 13, 2007
    #4
  5. Howard wrote:
    >
    > "Gaijinco" <> wrote in message
    > news:...
    >
    >> I'm having a weird error compiling a multiple file project:
    >>
    >> I have three files:
    >>
    >> tortuga.h where I have declared 5 global variables and prototypes for
    >> some functions.
    >> tortuga.cpp where I implement all of the functions of tortuga.h
    >> main.cpp where I use the functions implemented in tortuga.cpp
    >>
    >> I create the objetc file without problem. But when I try to compile
    >> "main.cpp" it says:
    >>
    >> "multiple declaration of..." for each one of the global variables.
    >>
    >> What am I doing wrong?

    >
    >
    > Don't declare global variables in header files. Instead, if you want
    > them to be visible from multiple source files, then move the
    > declarations to a source file, and then declare them as "extern" in the
    > header file.


    What is the difference between "declare" and "define" ?

    >
    > Better yet, don't use global variables at all. But that's another
    > question...


    Globals are a very important part of the language. You can't do without
    them "at all". However, they are often misused but that is still no
    reason to outlaw them.

    Reasons globals are good.

    a) Constants - objects that do not change throughout the life of the
    program (at least after main()) is called.

    b) Registries - objects that are a list of items for that executable
    (not specific to an instance of an object). e.g. Factories, plugins,
    command line specifiers, initializer specifiers, etc...
    Gianni Mariani, Apr 13, 2007
    #5
  6. Gaijinco

    Howard Guest

    ----- Original Message -----
    From: "Gaijinco" <>
    Newsgroups: comp.lang.c++
    Sent: Friday, April 13, 2007 3:24 PM
    Subject: Re: Error compiling multiple file projects:


    > It appears that you're including the header twice somehow.
    >
    > Should be enough to use
    >
    > #ifndef FILE_H
    > #define FILE_H
    >
    > #endif
    >
    > to stop including the header more than once?
    >


    That will stop errors relating to multiple type definitions, but that's not
    his problem. He's getting "multiple declaration" errors on global variables
    defined in a header. If he fixes the error of the multiple declarations by
    using include guards, then what will happen is that he'll get "variable
    undefined" errors from the second and subsequent files attempting to use
    those variables. The way to allow multiple compilation units to see a
    global variable definition is to use the "extern" keyword in a header file,
    and move the declaration/definition into an implementation file.

    -Howard

    (P.S., sorry for replying directly to you first; I'm still getting used to
    this new newsreader.)
    Howard, Apr 16, 2007
    #6
  7. Gaijinco

    Howard Guest

    ----- Original Message -----
    From: "Gianni Mariani" <>
    Newsgroups: comp.lang.c++
    Sent: Friday, April 13, 2007 3:28 PM
    Subject: Re: Error compiling multiple file projects:


    > Howard wrote:
    >>
    >> "Gaijinco" <> wrote in message
    >> news:...
    >>
    >>> I'm having a weird error compiling a multiple file project:
    >>>
    >>> I have three files:
    >>>
    >>> tortuga.h where I have declared 5 global variables and prototypes for
    >>> some functions.
    >>> tortuga.cpp where I implement all of the functions of tortuga.h
    >>> main.cpp where I use the functions implemented in tortuga.cpp
    >>>
    >>> I create the objetc file without problem. But when I try to compile
    >>> "main.cpp" it says:
    >>>
    >>> "multiple declaration of..." for each one of the global variables.
    >>>
    >>> What am I doing wrong?

    >>
    >>
    >> Don't declare global variables in header files. Instead, if you want
    >> them to be visible from multiple source files, then move the declarations
    >> to a source file, and then declare them as "extern" in the header file.

    >
    > What is the difference between "declare" and "define" ?


    Are you asking, or pointing out a mistake in my usage? I'm never quite sure
    how to refer to asomething like this:

    const int count = 36;

    It's both a declaration AND a definition, right?

    >
    >>
    >> Better yet, don't use global variables at all. But that's another
    >> question...

    >
    > Globals are a very important part of the language. You can't do without
    > them "at all". However, they are often misused but that is still no
    > reason to outlaw them.
    >
    > Reasons globals are good.
    >
    > a) Constants - objects that do not change throughout the life of the
    > program (at least after main()) is called.
    >
    > b) Registries - objects that are a list of items for that executable (not
    > specific to an instance of an object). e.g. Factories, plugins, command
    > line specifiers, initializer specifiers, etc...


    Agreed. I probably should have said "avoid them" instead of "don't use
    [them] at all". But when someone talks about declaring a bunch of "global
    variables", it's most likely not a good design.

    -Howard

    (P.S., sorry for replying directly to you first; I'm still getting used to
    this new newsreader.)
    Howard, Apr 16, 2007
    #7
    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. jaredea

    Can't create projects / open projects

    jaredea, Jun 9, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    572
    =?Utf-8?B?TWFyaWU=?=
    Aug 10, 2005
  2. Replies:
    4
    Views:
    943
    Timo Stamm
    Feb 24, 2006
  3. Replies:
    0
    Views:
    409
  4. Garrett Cooper
    Replies:
    0
    Views:
    542
    Garrett Cooper
    Feb 24, 2009
  5. Garrett Cooper
    Replies:
    0
    Views:
    487
    Garrett Cooper
    Feb 24, 2009
Loading...

Share This Page