Linker Symbol resolution in C/C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VanKha
    New Member
    • May 2007
    • 21

    Linker Symbol resolution in C/C++

    Hi everyone ,
    I've just read a book on Computer Systems.(Comput er Systems:A programmer's perspective ) . On the part of "Linking" , the author talks about strong/weak symbol and as an example for the rule "Given a strong symbol and weak symbols , choose the strong one" , he gives the following C modules
    Code:
    /* foo5.c */
    #include<stdio.h>
    void f();
    int x=15213,y=15212;
    int main()
    {
        f();
        printf("%d %d",x,y);
        return 0;
    }
    
    /* bar5.c */
    double x;
    void f()
    { 
       x=-0.0;
    }
    And points out that the symbol in foo5.c will be chosen.And the program wil run without errors but has unexpected result (y being overwritten with the **** value ? ) . That's right ! But only if I compile with gcc ( my OS is Fedora 5 ) ! With g++ it will generate the multiple definition error ! (Of course , with g++ , I've change stdio->iostream, etc...).
    So is it because the C++ compiler/linker is "smarter" ? So , what's the theories and rules added in c++ linker (if any ?)
    One more question , I still dont understand this : if the symbol for x in foo5.c has been chosen , what causes y to be overwritten ? The author of the book said about int-4 byte and double-8 byte ,... But once x has been chosen as int , why double is concerned here ? I mean , why not the linker just throw away other definitions and treat x as int always from that time on ?
    Replies appreciated ;
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This won't even compile as C code because there are two global variables named x.

    Comment

    • VanKha
      New Member
      • May 2007
      • 21

      #3
      Have you tried it ? I compiled it successfully !
      It has to do with strong symbol / weak symbol resolution as I've said .
      A lot of documents pop out on this topic even if you use Google .
      Here is one picked randomly : https://www.andrew.cmu.edu/user/ngm/...nk-answers.pdf
      ( Read from "Linking Summary" page 6 )
      And there's a chapter in the book I mentioned ...
      I'm telling this in case you think I'm playing a joke :D !

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I tried it. There are two global variables named x and it dies in the link.

        If you have a compiler that accepts this, then get a better compiler.

        Comment

        • VanKha
          New Member
          • May 2007
          • 21

          #5
          But...What about the the references I mentioned ?
          Btw,I use gcc(again,my OS is Linux Fedora 5)

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            sicarie@hostnam e:~/bin$ gcc foo5.c
            foo5.c:12: error: conflicting types for ‘x’
            foo5.c:3: error: previous definition of ‘x’ was here
            sicarie@hostnam e:~/bin$ gcc -v
            gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

            Fedora's current version now is 8, I believe.

            Comment

            Working...