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

I

izhar.wallach

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;
}
 
M

mlimber

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
 
P

Puppet_Sock

(e-mail address removed) 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
 
I

izhar.wallach

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.
 
O

Old Wolf

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top