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 -
/* 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 ;
5 3312
This won't even compile as C code because there are two global variables named x.
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 !
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.
But...What about the the references I mentioned ?
Btw,I use gcc(again,my OS is Linux Fedora 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 Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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();
}
|
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.
|
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 :...
|
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
| |
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...
|
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 :...
|
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
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |