Thread: external linkage in a block?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    external linkage in a block?

    Hi

    I was reading ISO C99 standard about linkage.

    Code:
    For an identifier declared with the storage-class specifier extern in a scope in which a
    prior declaration of that identifier is visible, 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.
    So if I wrote

    Code:
    int main(void)
    {
         int a;  //1st
         extern int a; //2nd
         printf("%d\n", a);
    }
    The 1st a has no linkage. According to the standard, after 2nd declaration, 'a' has external linkage? Then why I can't find its information in the symbol table with the nm command?

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What compiler errors are you getting? That should be your first clue.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by slingerland3g View Post
    What compiler errors are you getting? That should be your first clue.
    There's no compiler error in my case. Also I don't think this should be an error. It is a syntactic problem. I want to understand what the standard really means. The standard states 'a' is external linkage so I think it should be seen using the nm command as an external symbol. But I can't see it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Really? If I run gcc on your file, I get:
    Code:
    temp.c: In function ‘main’:
    temp.c:4: error: extern declaration of ‘a’ follows declaration with no linkage
    temp.c:3: error: previous declaration of ‘a’ was here
    temp.c:5: warning: implicit declaration of function ‘printf’
    temp.c:5: warning: incompatible implicit declaration of built-in function ‘printf’
    temp.c:3: warning: unused variable ‘a’
    temp.c:6: warning: control reaches end of non-void function

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by password636 View Post
    There's no compiler error in my case. Also I don't think this should be an error. It is a syntactic problem. I want to understand what the standard really means. The standard states 'a' is external linkage so I think it should be seen using the nm command as an external symbol. But I can't see it.
    In my compiler too I get the following error.
    Error 1 error C2086: 'int a' : redefinition
    And I guess it's because int a is a definition and when the compiler encounters extern declaration, it assumes that 'a' has been already defined somewhere. So it throws redefinition error.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by tabstop View Post
    Really? If I run gcc on your file, I get:
    Code:
    temp.c: In function ‘main’:
    ...
    Here is my output:
    Code:
    [tester@localhost apue2]$ cat test.c
    
    int main(void)
    {
         int a;  //1st
         extern int a; //2nd
         printf("%d\n", a);
    }
    [tester@localhost apue2]$ gcc test.c -Wall
    test.c: In function `main':
    test.c:6: warning: implicit declaration of function `printf'
    test.c:7: warning: control reaches end of non-void function
    [tester@localhost apue2]$ gcc --version
    gcc (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)
    Copyright (C) 2004 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    [tester@localhost apue2]$

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM