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
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 ;
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;
}
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 ;
Comment