external variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Bozanich

    external variables

    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

  • Buster

    #2
    Re: external variables

    Adam Bozanich wrote:[color=blue]
    > 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:[/color]
    [snip][color=blue]
    > How can I get around this issue?[/color]

    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.

    Comment

    • John Harrison

      #3
      Re: external variables


      "Adam Bozanich" <abozan01@ccsf. edu> wrote in message
      news:Pine.HPX.4 .44.04041921130 30.12110-100000@hills.cc sf.cc.ca.us...[color=blue]
      > 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[/color]
      as[color=blue]
      > c++. Here's an example:
      >[/color]

      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


      Comment

      • Buster

        #4
        Re: external variables

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

        No. In C there are tentative definitions.



        --
        Regards,
        Buster.

        Comment

        • John Harrison

          #5
          [OT] Re: external variables


          "Buster" <noone@nowhere. com> wrote in message
          news:c63sm5$5s7 $1@news8.svr.po l.co.uk...[color=blue]
          > John Harrison wrote:
          >[color=green]
          > > The rules in C and C++
          > > are essentially the same, you can only have one definition but as many
          > > declarations as you like.[/color]
          >
          > No. In C there are tentative definitions.
          >
          >[/color]
          http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM[color=blue]
          >
          > --
          > Regards,
          > Buster.[/color]

          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


          Comment

          • Jack Klein

            #6
            Re: external variables

            On Tue, 20 Apr 2004 20:11:56 +0100, Buster <noone@nowhere. com> wrote
            in comp.lang.c++:
            [color=blue]
            > John Harrison wrote:
            >[color=green]
            > > The rules in C and C++
            > > are essentially the same, you can only have one definition but as many
            > > declarations as you like.[/color]
            >
            > No. In C there are tentative definitions.
            >
            > http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM[/color]

            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.l earn.c-c++

            Comment

            • Buster

              #7
              Re: external variables

              Jack Klein wrote:[color=blue]
              > On Tue, 20 Apr 2004 20:11:56 +0100, Buster <noone@nowhere. com> wrote
              > in comp.lang.c++:
              >[color=green]
              >>John Harrison wrote:
              >>[color=darkred]
              >>>The rules in C and C++
              >>>are essentially the same, you can only have one definition but as many
              >>>declaratio ns as you like.[/color]
              >>
              >>No. In C there are tentative definitions.
              >>
              >>http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM[/color]
              >
              > 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.[/color]

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

              --
              Regards,
              Buster.

              Comment

              Working...