I get an "undefined reference" error with a recent gcc 4.2 when linking to a class whose header contains an additional namespace that is references from that class. This is a very recent error. It started to occur between 20060626 and 20060709. (sid)1690:tbm@reyes: ~/src/t] make rm -f *.o g++-4.1 -c t.cc ; g++-4.1 -c a.c ; g++-4.1 a.o t.o rm -f *.o g++-4.2 -c t.cc ; g++-4.2 -c a.c ; g++-4.2 a.o t.o a.o: In function `main': a.c:(.text+0x18): undefined reference to `test::MonitorHandle::MonitorHandle()' collect2: ld returned 1 exit status (sid)1691:tbm@reyes: ~/src/t] cat t.h namespace test { namespace { class SignalProxy_Monitor; } class MonitorHandle { public: MonitorHandle(); private: SignalProxy_Monitor* proxy_; }; } (sid)1693:tbm@reyes: ~/src/t] cat t.cc #include "t.h" namespace test { MonitorHandle::MonitorHandle() {} } (sid)1694:tbm@reyes: ~/src/t] cat a.c #include "t.h" int main(void) { test::MonitorHandle handle; return 0; }
This was caused by: r115086 | jason | 2006-06-30 03:15:56 +0200 (Fri, 30 Jun 2006) | 58 lines
The code is invalid as the anonymous namespace causes an ODR violation.
To better explain: test::MonitorHandle references a class in an anonymous namespace which causes that class to be different in each TU. Since ODR rules say the class needs to be the same across TUs so we declare test::MonitorHandle also a local class.
*** Bug 28370 has been marked as a duplicate of this bug. ***
*** Bug 28438 has been marked as a duplicate of this bug. ***