473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Linker Symbol resolution in C/C++

21
Hi everyone ,
I've just read a book on Computer Systems.(Computer 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
Expand|Select|Wrap|Line Numbers
  1. /* foo5.c */
  2. #include<stdio.h>
  3. void f();
  4. int x=15213,y=15212;
  5. int main()
  6. {
  7.     f();
  8.     printf("%d %d",x,y);
  9.     return 0;
  10. }
  11.  
  12. /* bar5.c */
  13. double x;
  14. void f()
  15.    x=-0.0;
  16. }
  17.  
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 ;
Feb 25 '08 #1
5 3282
weaknessforcats
9,208 Expert Mod 8TB
This won't even compile as C code because there are two global variables named x.
Feb 25 '08 #2
VanKha
21
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 : http://www.andrew.cmu.edu/user/ngm/1...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 !
Feb 26 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 26 '08 #4
VanKha
21
But...What about the the references I mentioned ?
Btw,I use gcc(again,my OS is Linux Fedora 5)
Feb 27 '08 #5
sicarie
4,677 Expert Mod 4TB
sicarie@hostname:~/bin$ gcc foo5.c
foo5.c:12: error: conflicting types for ‘x’
foo5.c:3: error: previous definition of ‘x’ was here
sicarie@hostname:~/bin$ gcc -v
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)

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

http://fedoraproject.org/get-fedora
Feb 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: KS | last post by:
I am getting linker error for a code that was tested and running. I opened the project in VS.NET 2003 and now i get these errors - I am sure it is some options issue. Can anyone please help? ...
1
by: Srikanth Madani | last post by:
Hi all, Suppose I have a C file fun1.c containing two functions: __________ void func1() { printf("\nIn function func1()\n"); } void func2() { func3(); }
2
by: Pradeep | last post by:
Hi all, I m basically trying to understand the linking/loading process, please bear with me as i have specified my question in an unclear fashion. Any document that u think can clarify my...
4
by: Joel Whitehouse | last post by:
Hello All, I have a Visual C++ 2003 dll solution in a directory called "Antenna Test Range Control", and when I comile it, I get the error LNK1104: cannot open file "antenna.obj". When I remove...
3
by: ralphsieminsky | last post by:
A project compiles fine under VS 2005 RC without the /clr option. However, when /clr is turned on several errors appear: - A symbol exported from a DLL is not found by another DLL referencing...
3
by: Chucker | last post by:
Hi Folks, I got a Wrapper Dll around a native C++ static library. In .NET 1.1 this worked fine. When moving to .NET 2.0 I get a couple of unresolved externals / linker errors: Error 16 error...
0
by: VivekR | last post by:
I have a MFC application developed using VC++ 5. Recently I ported that to VC++ 7.1 and now I am trying to compile the MFC application with /CLR under VC++ 7.1. And I get linker errors referring to...
1
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
1
by: iiitsunny | last post by:
i have ported one project which was working in VC6 to VC7. Now the project is working fine in VC7 also. I have made some additions in VC7 environment only. But the problem is that the project is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.