By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.