Re: Linkage

Discussion in 'C Programming' started by Ike Naar, Jan 26, 2013.

  1. Ike Naar

    Ike Naar Guest

    On 2013-01-26, Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
    > Hi,
    > In gcc-4.7 C99:
    >
    > extern int a;
    > static int a;
    >
    > I get: error: static declaration of 'a' follows non-static declaration
    >
    >
    > In WG14/N1256, 6.2.7 p4 seems to say this should not be an error.
    >
    >
    > 6.2.2 p4:
    >
    > "For an identifier declared with the storage-class specifier extern in a scope
    > in which a prior declaration of that identifier is visible,(23) if the prior
    > declaration specifies internal or external linkage, the linkage of the
    > identifier at the later declaration is the same as the linkage specified at the
    > prior declaration. If no prior declaration is visible, or if the prior
    > declaration specifies no linkage, then the identifier has external linkage."
    >
    >
    > 6.2.2 p7:
    >
    > "If, within a translation unit, the same identifier appears with both internal
    > and external linkage, the behavior is undefined."
    >
    >
    > 6.2.7 p4:
    >
    > "For an identifier with internal or external linkage declared in a scope in
    > which a prior declaration of that identifier is visible,(47) if the prior
    > declaration specifies internal or external linkage, the type of the identifier
    > at the later declaration becomes the composite type."


    With

    extern int a;
    static int a;

    the extern declaration is not in a scope in which a prior declaration
    is visible (the static declaration appears later in the program).
    So the identifier is declared with external linkage.
    Then, when the static declaration appears, the identifier is declared
    with internal linkage, and now the same identifier appears with
    both internal and external linkage.

    You can have both declarations if you change the order:

    static int a;
    extern int a;

    in which case the identifier will have internal linkage.
     
    Ike Naar, Jan 26, 2013
    #1
    1. Advertising

  2. Ike Naar

    Shao Miller Guest

    On 1/26/2013 02:12, Russell Shaw wrote:
    >
    > My mistake was seeing "composite" as combining "static" and "extern" as
    > in 6.2.2 p4.
    >


    Storage-class specifiers are not part of a type. You might notice that
    'typedef' is a storage-class specifier, too.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
     
    Shao Miller, Jan 26, 2013
    #2
    1. Advertising

  3. Ike Naar

    James Kuyper Guest

    On 01/26/2013 02:12 AM, Russell Shaw wrote:
    > On 26/01/13 17:52, Ike Naar wrote:
    >> On 2013-01-26, Russell Shaw<rjshawN_o@s_pam.netspace.net.au> wrote:
    >>> Hi,
    >>> In gcc-4.7 C99:
    >>>
    >>> extern int a;
    >>> static int a;
    >>>
    >>> I get: error: static declaration of 'a' follows non-static declaration
    >>>
    >>>
    >>> In WG14/N1256, 6.2.7 p4 seems to say this should not be an error.
    >>>
    >>>
    >>> 6.2.2 p4:
    >>>
    >>> "For an identifier declared with the storage-class specifier extern in a
    >>> scope in which a prior declaration of that identifier is visible,(23) if
    >>> the prior declaration specifies internal or external linkage, the linkage
    >>> of the identifier at the later declaration is the same as the linkage
    >>> specified at the prior declaration. If no prior declaration is visible, or
    >>> if the prior declaration specifies no linkage, then the identifier has
    >>> external linkage."
    >>>
    >>>
    >>> 6.2.2 p7:
    >>>
    >>> "If, within a translation unit, the same identifier appears with both
    >>> internal and external linkage, the behavior is undefined."
    >>>
    >>>
    >>> 6.2.7 p4:
    >>>
    >>> "For an identifier with internal or external linkage declared in a scope
    >>> in which a prior declaration of that identifier is visible,(47) if the
    >>> prior declaration specifies internal or external linkage, the type of the
    >>> identifier at the later declaration becomes the composite type."

    >>
    >> With
    >>
    >> extern int a;
    >> static int a;
    >>
    >> the extern declaration is not in a scope in which a prior declaration
    >> is visible (the static declaration appears later in the program). So the
    >> identifier is declared with external linkage. Then, when the static
    >> declaration appears, the identifier is declared with internal linkage, and
    >> now the same identifier appears with both internal and external linkage.
    >>
    >> You can have both declarations if you change the order:
    >>
    >> static int a;
    >> extern int a;
    >>
    >> in which case the identifier will have internal linkage.

    >
    > Hmm, so "becomes the composite type" really means apply the function
    > "composite_type()", where composite_type() is a function containing 6.2.7 p3.
    >
    > My mistake was seeing "composite" as combining "static" and "extern" as in 6.2.2 p4.


    Well, there was also a mistake involved if you thought that 6.2.2p4
    applied to your example. When the extern declaration comes first,
    6.2.2p4 doesn't apply, since the static declaration is not in scope yet,
    but 6.2.2p7 does apply, rendering everything else irrelevant.

    When the static declaration comes first, 6.2.2p4 applies, with the
    result that 6.2.2p7 doesn't (since it has only internal linkage, the
    'extern' keyword notwithstanding), thereby allowing 6.2.7p4 to be relevant.
    --
    James Kuyper
     
    James Kuyper, Jan 26, 2013
    #3
    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. Sönke Greve

    TabStrip Control with direct linkage?

    Sönke Greve, Jan 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    354
    Sönke Greve
    Jan 22, 2006
  2. Thomas Dorris

    JNI linkage issue

    Thomas Dorris, Jan 31, 2004, in forum: Java
    Replies:
    2
    Views:
    3,126
    Thomas Dorris
    Feb 2, 2004
  3. Mary
    Replies:
    1
    Views:
    4,037
    Thomas Fritsch
    Oct 11, 2004
  4. Don@NoSpam

    Devious linkage to enter Web site

    Don@NoSpam, Jul 5, 2004, in forum: HTML
    Replies:
    3
    Views:
    491
    brucie
    Jul 5, 2004
  5. Replies:
    1
    Views:
    609
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page