multiple global variable definitions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    multiple global variable definitions

    Suppose I declare a global variable

    int g;

    in two different files say a.c which has main() and b.c

    When I compile them to build an executable under gcc in Redhat Linux
    with the command

    gcc -std=c99 -pedantic -Wall -Wextra a.c b.c

    there is no linker error.

    I thought there should be linker error because of two definitions for
    the same global variable.

    Where am I going wrong ?

  • Fred Kleinschmidt

    #2
    Re: multiple global variable definitions


    <subramanian100 in@yahoo.comwro te in message
    news:1174313429 .604309.128500@ e65g2000hsc.goo glegroups.com.. .
    Suppose I declare a global variable
    >
    int g;
    >
    in two different files say a.c which has main() and b.c
    >
    When I compile them to build an executable under gcc in Redhat Linux
    with the command
    >
    gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
    >
    there is no linker error.
    >
    I thought there should be linker error because of two definitions for
    the same global variable.
    >
    Where am I going wrong ?
    >
    Global variables declared in the .c file have file scope. You have two
    different variables, the same as declaring the same "int i" as a local
    variable in two different functions. They have the same name, but
    do not overlap in scope.

    There would be only one variable if one file declared the variable to
    be"extern".


    Comment

    • Richard Heathfield

      #3
      Re: multiple global variable definitions

      subramanian100i n@yahoo.com, India said:
      Suppose I declare a global variable
      >
      int g;
      >
      in two different files say a.c which has main() and b.c
      >
      When I compile them to build an executable under gcc in Redhat Linux
      with the command
      >
      gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
      >
      there is no linker error.
      >
      I thought there should be linker error because of two definitions for
      the same global variable.
      >
      Where am I going wrong ?
      They are only "tentative" definitions, not actual definitions. There is,
      however, no such thing as a "tentative initialisation" , so change them
      both to:

      int g = 0;

      and watch the compiler (or rather the linker) choke.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at the above domain, - www.

      Comment

      • Roberto Waltman

        #4
        Re: multiple global variable definitions

        Richard Heathfield wrote:
        >subramanian100 in@yahoo.com, India said:
        >Suppose I declare a global variable
        >int g;
        >in two different files say a.c which has main() and b.c
        >there is no linker error.
        >>
        >I thought there should be linker error because of two definitions for
        >the same global variable.
        >
        >They are only "tentative" definitions, not actual definitions. There is,
        >however, no such thing as a "tentative initialisation" , so change them
        >both to:
        >
        >int g = 0;
        >
        >and watch the compiler (or rather the linker) choke.
        Trying to understand the standard, from N1124:

        "6. Language
        ....
        6.9 External Definitions
        ....
        6.9.2 External object definitions
        Semantics
        ....
        2 A declaration of an identifier for an object
        that has file scope without an initializer, and
        without a storage-class specifier or with the
        storage-class specifier static, constitutes a
        tentative definition. If a translation unit
        contains one or more tentative definitions for an
        identifier, and the translation unit contains no
        external definition for that identifier, then
        the behavior is exactly as if the translation unit
        contains a file scope declaration of that
        identifier, with the composite type as of the end
        of the translation unit, with an initializer equal
        to 0."

        Although I expect the behavior that the OP
        described, the paragraph above seems to contradict
        it:

        int g; "is exactly as" int g = 0;

        What I am missing?

        Also, can somebody clarify the meaning of "with
        the composite type as of the end of the
        translation unit" (I am not sure if I am
        struggling with an unclear text, of with the fact
        that English is not my native language.)

        Roberto Waltman

        [ Please reply to the group,
        return address is invalid ]

        Comment

        • Flash Gordon

          #5
          Re: multiple global variable definitions

          Fred Kleinschmidt wrote, On 19/03/07 14:36:
          <subramanian100 in@yahoo.comwro te in message
          news:1174313429 .604309.128500@ e65g2000hsc.goo glegroups.com.. .
          >Suppose I declare a global variable
          >>
          >int g;
          >>
          >in two different files say a.c which has main() and b.c
          >>
          >When I compile them to build an executable under gcc in Redhat Linux
          >with the command
          >>
          >gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
          >>
          >there is no linker error.
          >>
          >I thought there should be linker error because of two definitions for
          >the same global variable.
          >>
          >Where am I going wrong ?
          Your mistake is assuming that the linker is required to catch your
          error. It isn't.
          Global variables declared in the .c file have file scope. You have two
          different variables, the same as declaring the same "int i" as a local
          variable in two different functions. They have the same name, but
          do not overlap in scope.
          >
          There would be only one variable if one file declared the variable to
          be"extern".
          With neither being declared as extern it invokes undefined behaviour, so
          anything is allowed to happen and the implementation (compiler/linker)
          is not required to generate an error for it. The most common things to
          happen are for the two "int i" definitions to be merged or for an error
          to be generated.
          --
          Flash Gordon

          Comment

          • Flash Gordon

            #6
            Re: multiple global variable definitions

            Richard Heathfield wrote, On 19/03/07 15:52:
            subramanian100i n@yahoo.com, India said:
            >
            >Suppose I declare a global variable
            >>
            >int g;
            >>
            >in two different files say a.c which has main() and b.c
            >>
            >When I compile them to build an executable under gcc in Redhat Linux
            >with the command
            >>
            >gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
            >>
            >there is no linker error.
            >>
            >I thought there should be linker error because of two definitions for
            >the same global variable.
            >>
            >Where am I going wrong ?
            >
            They are only "tentative" definitions, not actual definitions.
            Irrelevant as far as the standard is concerned. As soon as it reaches
            the end of the translation unit it is as if there was a definition with
            an initialiser equal to 2. Section 6.9.2 paragraph 2 of N1124.
            There is,
            however, no such thing as a "tentative initialisation" , so change them
            both to:
            >
            int g = 0;
            >
            and watch the compiler (or rather the linker) choke.
            It may not since it is undefined behaviour either way. See above and
            section 6.9 paragraph 5.

            To the OP, it does not generate an error because it is not required to.
            It also is not required to "work", it could do anything.

            <OT>
            If using gcc you might want to investigate the -fno-common option.
            </OT>
            --
            Flash Gordon

            Comment

            • Richard Heathfield

              #7
              Re: multiple global variable definitions

              Flash Gordon said:
              Richard Heathfield wrote, On 19/03/07 15:52:
              >subramanian100i n@yahoo.com, India said:
              >>
              >>Suppose I declare a global variable
              >>>
              >>int g;
              >>>
              >>in two different files say a.c which has main() and b.c
              >>>
              >>When I compile them to build an executable under gcc in Redhat Linux
              >>with the command
              >>>
              >>gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
              >>>
              >>there is no linker error.
              >>>
              >>I thought there should be linker error because of two definitions
              >>for the same global variable.
              >>>
              >>Where am I going wrong ?
              >>
              >They are only "tentative" definitions, not actual definitions.
              >
              Irrelevant as far as the standard is concerned.
              I'll take that hit...
              As soon as it reaches
              the end of the translation unit it is as if there was a definition
              with an initialiser equal to 2.
              ....but this puzzles me. Did you mean 0?

              --
              Richard Heathfield
              "Usenet is a strange place" - dmr 29/7/1999

              email: rjh at the above domain, - www.

              Comment

              • Wojtek Lerch

                #8
                Re: multiple global variable definitions

                "Roberto Waltman" <usenet@rwaltma n.netwrote in message
                news:mnctv2h2mj d2j12uu727041mq sgf5dmvj4@4ax.c om...
                Richard Heathfield wrote:
                >>subramanian10 0in@yahoo.com, India said:
                >>Suppose I declare a global variable
                >>int g;
                >>in two different files say a.c which has main() and b.c
                >>there is no linker error.
                >>>
                >>I thought there should be linker error because of two definitions for
                >>the same global variable.
                The C standard doesn't require a linker error if you have two definitions of
                the same global variable. It's undefined behaviour (6.9#5). Whether you
                get an error or not is up to the implementation.
                >>They are only "tentative" definitions, not actual definitions. [...]
                ....
                Although I expect the behavior that the OP
                described, the paragraph above seems to contradict
                it:
                >
                int g; "is exactly as" int g = 0;
                >
                What I am missing?
                Nothing. If a translation unit contains a tentative definition, then either
                it also contains a "real" definition, or the behaviour is as if it contained
                a "real" definition. Either way, having two translation units that define
                the same identifier with external linkage (tentatively or otherwise) is
                undefined behaviour.
                Also, can somebody clarify the meaning of "with
                the composite type as of the end of the
                translation unit" (I am not sure if I am
                struggling with an unclear text, of with the fact
                that English is not my native language.)
                Consider this translation unit:

                int arr[ 10 ];
                int arr[];

                It contains two tentative definitions that declare "arr" with two different
                types: "int[10]" and "int[]". The composite type is "arr[10]", and
                therefore the behaviour is as if "arr" were defined like this:

                int arr[10] = { 0 };

                rather than like this:

                int arr[] = { 0 };

                Comment

                • Mark McIntyre

                  #9
                  Re: multiple global variable definitions

                  On Mon, 19 Mar 2007 17:26:50 +0000, in comp.lang.c , Richard
                  Heathfield <rjh@see.sig.in validwrote:
                  >Flash Gordon said:
                  >
                  >As soon as it reaches
                  >the end of the translation unit it is as if there was a definition
                  >with an initialiser equal to 2.
                  >
                  >...but this puzzles me. Did you mean 0?
                  2 would be more entertaining though, don't you think? Shame its not
                  allowed...

                  --
                  Mark McIntyre

                  "Debugging is twice as hard as writing the code in the first place.
                  Therefore, if you write the code as cleverly as possible, you are,
                  by definition, not smart enough to debug it."
                  --Brian Kernighan

                  Comment

                  • Flash Gordon

                    #10
                    Re: multiple global variable definitions

                    Richard Heathfield wrote, On 19/03/07 17:26:
                    Flash Gordon said:
                    >
                    >Richard Heathfield wrote, On 19/03/07 15:52:
                    >>subramanian100i n@yahoo.com, India said:
                    >>>
                    >>>Suppose I declare a global variable
                    >>>>
                    >>>int g;
                    >>>>
                    >>>in two different files say a.c which has main() and b.c
                    >>>>
                    >>>When I compile them to build an executable under gcc in Redhat Linux
                    >>>with the command
                    >>>>
                    >>>gcc -std=c99 -pedantic -Wall -Wextra a.c b.c
                    >>>>
                    >>>there is no linker error.
                    >>>>
                    >>>I thought there should be linker error because of two definitions
                    >>>for the same global variable.
                    >>>>
                    >>>Where am I going wrong ?
                    >>They are only "tentative" definitions, not actual definitions.
                    >Irrelevant as far as the standard is concerned.
                    >
                    I'll take that hit...
                    Just goes to show that those claiming you never accept a correction are
                    wrong. Something I already knew.
                    >As soon as it reaches
                    >the end of the translation unit it is as if there was a definition
                    >with an initialiser equal to 2.
                    >
                    ...but this puzzles me. Did you mean 0?
                    Yes, so that's 1 all then :-)
                    --
                    Flash Gordon

                    Comment

                    • David Thompson

                      #11
                      Re: multiple global variable definitions

                      On Mon, 19 Mar 2007 14:36:43 GMT, "Fred Kleinschmidt"
                      <fred.l.kleinms chmidt@boeing.c omwrote:
                      >
                      <subramanian100 in@yahoo.comwro te in message
                      news:1174313429 .604309.128500@ e65g2000hsc.goo glegroups.com.. .
                      Suppose I declare a global variable

                      int g;

                      in two different files say a.c [...] and b.c [...]
                      there is no linker error.

                      I thought there should be linker error because of two definitions for
                      the same global variable.

                      Where am I going wrong ?
                      Global variables declared in the .c file have file scope. You have two
                      different variables, the same as declaring the same "int i" as a local
                      variable in two different functions. They have the same name, but
                      do not overlap in scope.
                      >
                      Not really. Each of these is a tentative definition, and assuming
                      there is no other definition of the same name (at file scope) with an
                      initializer, the tentative definition becomes/produces a (definitive?)
                      definition. So you have two t.u.s both defining the same object (name)
                      with external linkage, which is Undefined Behavior. The implementation
                      is allowed to diagnose this, and in some implementations (and some
                      options of some implementations ) it does produce a linker error, but
                      this is not required, and some don't.

                      If you declared/defined it with storage class 'static' in each t.u.,
                      then you would (always) have two distinct objects.
                      There would be only one variable if one file declared the variable to
                      be"extern".
                      >
                      That is the right way to do it, yes. More precisely, one t.u. should
                      define it, which is accomplished if you give no storage class, ior if
                      you have an initializer (even if you also specify storage class
                      'extern'); and the other t.u. (or in general all other t.u.s) should
                      declare it with 'extern' and no initializer.

                      Comment

                      Working...