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