473,395 Members | 1,437 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,395 software developers and data experts.

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 1398
izhar.wall...@gmail.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...@gmail.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...@gmail.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
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
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...
4
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...
12
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...
22
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. ...
6
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...
4
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
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...
14
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.