473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linker odd behaviour while trying to assign objects from 2 different namespace

Hi all,
I've noticed an odd behavior of the linker while trying to compile the
code below.
The linking did not fail even though variable a1 (A2.cpp) was defined
as extern in namespace A2 while its actual deceleration was in
namespace A1 (main.cpp). Thus, the assignment a1 = &a2 (A2.cpp) should
have failed.
I used the following for compilation: g++ -Wall -pedantic -ansi
main.cpp A2.cpp
gcc version:
Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.5/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr
--with-local-prefix=/usr/local --infodir=/usr/share/info
--mandir=/usr/share/man --enable-languages=c,c++ ,f77,objc,java, ada
--disable-checking --libdir=/usr/lib --enable-libgcj
--with-slibdir=/lib --with-system-zlib --enable-shared
--enable-__cxa_atexit i586-suse-linux
Thread model: posix
gcc version 3.3.5 20050117 (prerelease) (SUSE Linux)

Is it a linker's bug?

Regards,
Izhar.

#include "A1.h"
#include "A2.h"
A1::A *a1;
int main(int argc,char** argv) {
a1->foo();
return 0;
}

// A1.h
namespace A1 {
class A {
public:
int a;
};
}

// A2.h
namespace A2 {
class A {
public:
A();
int a;
};
}

// A2.cpp
#include "A2.h"
A2::A a2;
extern A2::A *a1;
A2::A::A() {
a1 = &a2;
}

Nov 22 '05 #1
4 1431
izhar.wall...@g mail.com wrote:
Hi all,
I've noticed an odd behavior of the linker while trying to compile the
code below.
The linking did not fail even though variable a1 (A2.cpp) was defined
as extern in namespace A2 while its actual deceleration was in
namespace A1 (main.cpp). Thus, the assignment a1 = &a2 (A2.cpp) should
have failed.
I used the following for compilation: g++ -Wall -pedantic -ansi
main.cpp A2.cpp
gcc version:
Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.5/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr
--with-local-prefix=/usr/local --infodir=/usr/share/info
--mandir=/usr/share/man --enable-languages=c,c++ ,f77,objc,java, ada
--disable-checking --libdir=/usr/lib --enable-libgcj
--with-slibdir=/lib --with-system-zlib --enable-shared
--enable-__cxa_atexit i586-suse-linux
Thread model: posix
gcc version 3.3.5 20050117 (prerelease) (SUSE Linux)

Is it a linker's bug?

Regards,
Izhar.

#include "A1.h"
#include "A2.h"
A1::A *a1;
int main(int argc,char** argv) {
a1->foo();
return 0;
}

// A1.h
namespace A1 {
class A {
public:
int a;
};
}

// A2.h
namespace A2 {
class A {
public:
A();
int a;
};
}

// A2.cpp
#include "A2.h"
A2::A a2;
extern A2::A *a1;
A2::A::A() {
a1 = &a2;
}


I'd guess, yes, it's a bug (VC6 sp6 doesn't allow it). Perhaps someone
here can quote the Standard for you on having objects with external
linkage and with the same name but with different types.

Cheers! --M

Nov 22 '05 #2
izhar.wall...@g mail.com wrote:
[compiler command/message salad deleted]
Is it a linker's bug?

Regards,
Izhar.

#include "A1.h"
#include "A2.h"
A1::A *a1;
int main(int argc,char** argv) {
a1->foo();
return 0;
}

// A1.h
namespace A1 {
class A {
public:
int a;
};
}

// A2.h
namespace A2 {
class A {
public:
A();
int a;
};
}

// A2.cpp
#include "A2.h"
A2::A a2;
extern A2::A *a1;
A2::A::A() {
a1 = &a2;
}


I see a reference to foo(); but I don't see a declaration of it
anyplace.
My compiler agrees. It barfed over that.

Also, I don't see an A1.cpp anywhere. Was it omitted?

I think it may be a cut-n-paste error in creating your message to post.
Socks

Nov 22 '05 #3
The reference to foo() should be omitted.
It is not part of the code example. The main should be:
#include "A1.h"
#include "A2.h"
A1::A *a1;
int main(int argc,char** argv) {
return 0;
}

A1.cpp is empty (actually, it contains inculde for the header) and thus
was omitted.

Izhar.

Nov 22 '05 #4
izhar.wall...@g mail.com wrote:
I've noticed an odd behavior of the linker while trying to compile the
code below.
The linking did not fail even though variable a1 (A2.cpp) was defined
as extern in namespace A2 while its actual deceleration was in
namespace A1 (main.cpp). Thus, the assignment a1 = &a2 (A2.cpp) should
have failed.


You cause undefined behaviour by having a symbol's definition
not match the external declaration. The error is the same as:

// foo.cpp
extern int x;

int main() { printf("%d\n", x); }

// bar.cpp
double x = 5.0;

The linker isn't required to perform any type checking.

To help avoid this:
- put the 'extern' declaration in a header file, which is included
by the unit that declares the actual instance
- declare as 'static' any file-scope variables that aren't meant
to be accessed externally (such as your A1::A *a1).

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
2789
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
1
2261
by: adeht | last post by:
In his book, "Modern C++ Design", Andrei Alexandrescu presents a Generic Object Factory class (Loki::Factory). One of its advantages (also mentioned in the book) is being able to register the class in the factory without having a central place that needs to be modified each time we add a new type. For example, the registration can be done in the same compilation unit as the class implementation: (code like this has been presented in...
4
2086
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
12
801
by: Baloff | last post by:
Hello I have this linker error which makes me think that the definition file is not being seen by the linker, this code is taken from "Thinking in C++, 2nd Edition, Volume 1, Annotated Solutions Guide" Using Dev-C++ 4.9.9.2, The error is: undefined reference to ‘f(int)’ all the following files are in the same dir. thanks
22
2139
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name* binds
6
2475
by: Sampei120 | last post by:
When I go to compile a project I see this error undefined reference to `_imp___ZN3irr12createDeviceENS_5video13E_DRIVER_TYPEERKNS_4core11dimension2dIiEEjbbbPNS_14IEventReceiverEPKw' someone knows how can I do for delete this error??? Thanks
4
5881
by: Mark | last post by:
Hi, I'm trying to write some classes that kind of manage themselves. A linked list, that links different types of objects. Here's the code... object.h: --- class Object { int alt;
27
3862
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
14
6027
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9497
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,...
1
10110
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
9962
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
8992
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
7515
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
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.