473,811 Members | 3,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linker Symbol resolution in C/C++

21 New Member
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
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 3312
weaknessforcats
9,208 Recognized Expert Moderator Expert
This won't even compile as C code because there are two global variables named x.
Feb 25 '08 #2
VanKha
21 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Specialist
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.

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
1458
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? Linking... atls.lib(atlbase.obj) : error LNK2001: unresolved external symbol ___security_cookie PenPacketInfoDlg.obj : error LNK2019: unresolved external
1
1311
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
1717
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 doubts will also do, just specify the link of the doc. After reading some documents online, i have perceived that the some information about symbol resolution is built into the executable.
4
1836
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 teh spaces in my solution directory, I get the following errors: MastController.exp : error LNK2001: unresolved external symbol _Calibrate@8 MastController.exp : error LNK2001: unresolved external symbol _Initialize@8 MastController.exp :...
3
3304
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 it. The name of the symbol present in the DLL, as shown by depends.exe is ?Apply@ScreenContext@@SGPAV1@PAUHWND__@@@Z But the name of the symbol the linker looks for when
3
8092
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 LNK2028: unresolved token (0A000007) "extern "C" void __clrcall ___CxxCallUnwindDtor(void (__clrcall*)(void *),void *)" (?___CxxCallUnwindDtor@@$$J0YMXP6MXPAX@Z0@Z) referenced in function "public: virtual __thiscall...
0
1759
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 one of the libraries that the project uses. Without the /CLR, the project compiles and runs well. There is some library by name task.lib and the code refers to that lib, in the linker step i get the following linker errors:- Task.lib :...
1
3295
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, but throws a load of linker errors
1
10365
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 working in the release mode. I mean it is shooting off linking errors. I am required to generate an exe file and am looking for some suggestions!!! thank you all Linker Errors are something like this... error LNK2001: unresolved external...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.