external variables

Discussion in 'C++' started by Adam Bozanich, Apr 20, 2004.

  1. Hi all. I am porting a C program I wrote to C++. I am having some issues
    with external variables. In C, I can have global variables declared in
    common header files, but I get "multiple definition" errors when compiling as
    c++. Here's an example:


    /* BEGIN myhdr.h */
    int GLOBAL_INT;
    void func();
    /* END myhdr.h */

    /* BEGIN main.cpp */
    #include"myhdr.h"
    int main()
    {
    GLOBAL_INT = 1;
    func();
    return (0);
    }
    /* END main.cpp */

    /* BEGIN func.cpp */
    #include"myhdr.h"
    void func()
    {
    GLOBAL_INT = 2;
    }
    /* END func.cpp */

    g++ -c main.cpp
    g++ -c func.cpp
    g++ main.o func.o

    func.o(.bss+0x0): multiple definition of `GLOBAL_INT'
    main.o(.bss+0x0): first defined here
    gmake: *** [test] Error 1


    How can I get around this issue?

    Thanks,
    -Adam
    Adam Bozanich, Apr 20, 2004
    #1
    1. Advertising

  2. Adam Bozanich

    Buster Guest

    Adam Bozanich wrote:
    > Hi all. I am porting a C program I wrote to C++. I am having some issues
    > with external variables. In C, I can have global variables declared in
    > common header files, but I get "multiple definition" errors when compiling as
    > c++. Here's an example:

    [snip]
    > How can I get around this issue?


    Use "extern int GLOBAL_INT;" to declare the variable in the header file.
    Provide a definition, "int GLOBAL_INT;", in exactly one source file.

    --
    Regards,
    Buster.
    Buster, Apr 20, 2004
    #2
    1. Advertising

  3. "Adam Bozanich" <> wrote in message
    news:p...
    > Hi all. I am porting a C program I wrote to C++. I am having some issues
    > with external variables. In C, I can have global variables declared in
    > common header files, but I get "multiple definition" errors when compiling

    as
    > c++. Here's an example:
    >


    You don't understand the difference between a declaration and a definition,
    even your question mixes up these two different things.

    There is little difference here between C and C++ so I don't know what
    compiler you are using that lets you have multiple definitions in C, either
    you are mistaken or its a seriously bad C compiler. The rules in C and C++
    are essentially the same, you can only have one definition but as many
    declarations as you like.

    int GLOBAL_INT;

    The above is a definition, don't put it in a header file, put it in one
    source file.

    extern int GLOBAL_INT;

    The above is a declaration, put it in a common header file.

    You'll find that give or take a wrinkle or two, the same applies to C and
    C++.

    john
    John Harrison, Apr 20, 2004
    #3
  4. Adam Bozanich

    Buster Guest

    Buster, Apr 20, 2004
    #4
  5. [OT] Re: external variables

    "Buster" <> wrote in message
    news:c63sm5$5s7$...
    > John Harrison wrote:
    >
    > > The rules in C and C++
    > > are essentially the same, you can only have one definition but as many
    > > declarations as you like.

    >
    > No. In C there are tentative definitions.
    >
    >

    http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_022.HTM
    >
    > --
    > Regards,
    > Buster.


    Would two identical tentative definitions in different translations units
    cause a multiple definition error? I can see from the link that they would
    be OK in the same translation unit.

    The OP was claiming two tentative definitions in different translation units
    is OK in C, which seemed unlikely to me.

    john
    John Harrison, Apr 20, 2004
    #5
  6. Adam Bozanich

    Jack Klein Guest

    On Tue, 20 Apr 2004 20:11:56 +0100, Buster <> wrote
    in comp.lang.c++:

    > John Harrison wrote:
    >
    > > The rules in C and C++
    > > are essentially the same, you can only have one definition but as many
    > > declarations as you like.

    >
    > No. In C there are tentative definitions.
    >
    > http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_022.HTM


    You misunderstand tentative definitions in C. Tentative definitions
    are a concept that exists within a single translation unit only, they
    do not extend to linkage. At the end of a translation unit, if no
    non-tentative definition has appeared for a tentative one, the
    tentative one is turned into a specific definition.

    The OP's sample creates multiple definitions for the int variable
    object, one for each translation unit that includes the header. This
    happens to link without error on some implementations due to the model
    used by the linker.

    Nevertheless, providing more than one definition for an object with
    external linkage in a C program specifically invokes undefined
    behavior, and this is what the OP's sample does.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 21, 2004
    #6
  7. Adam Bozanich

    Buster Guest

    Jack Klein wrote:
    > On Tue, 20 Apr 2004 20:11:56 +0100, Buster <> wrote
    > in comp.lang.c++:
    >
    >>John Harrison wrote:
    >>
    >>>The rules in C and C++
    >>>are essentially the same, you can only have one definition but as many
    >>>declarations as you like.

    >>
    >>No. In C there are tentative definitions.
    >>
    >>http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_022.HTM

    >
    > You misunderstand tentative definitions in C. Tentative definitions
    > are a concept that exists within a single translation unit only, they
    > do not extend to linkage. At the end of a translation unit, if no
    > non-tentative definition has appeared for a tentative one, the
    > tentative one is turned into a specific definition.
    >
    > The OP's sample creates multiple definitions for the int variable
    > object, one for each translation unit that includes the header. This
    > happens to link without error on some implementations due to the model
    > used by the linker.
    >
    > Nevertheless, providing more than one definition for an object with
    > external linkage in a C program specifically invokes undefined
    > behavior, and this is what the OP's sample does.


    I'll take your word for it. Thanks! And sorry, everyone, for confusing
    matters.

    --
    Regards,
    Buster.
    Buster, Apr 21, 2004
    #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. Deb M.

    Post Variables to External Script

    Deb M., Jul 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    908
    David Waz...
    Jul 11, 2003
  2. Newsnet Customer

    c++: external variables

    Newsnet Customer, Aug 22, 2003, in forum: C++
    Replies:
    1
    Views:
    14,252
    Buster Copley
    Aug 22, 2003
  3. Scott Allen
    Replies:
    8
    Views:
    10,888
    Scott Allen
    May 2, 2004
  4. Replies:
    9
    Views:
    914
  5. Mellow Crow
    Replies:
    6
    Views:
    404
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page