unhelpful error message from linker in g++ 3.3.4
>Submitter-Id: net
>Originator: Edward Welbourne
>Organization:
>Confidential: no
>Synopsis: Linker error message is a triumph of misdirection
>Severity: non-critical
>Priority: low
>Category: c++
>Class: sw-bug
>Release: 3.3.4 (Debian 1:3.3.4-6sarge1) (Debian testing/unstable)
>Environment:
System: Linux whorl 2.4.26-1-686-smp #1 SMP Sat May 1 19:17:11 EST 2004 i686 GNU/Linux
Architecture: i686
host: i486-pc-linux-gnu
build: i486-pc-linux-gnu
target: i486-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux
>Description:
Feed the compiler the following erroneous code:
class Base { public: virtual void Method(void) /* = 0 */; }; // missing text
class Derived : public Base { public: virtual void Method(void) {} };
int main(void)
{
Derived thing;
Base *ptr = &thing;
ptr->Method();
return 0;
}
You will get an error message which makes no mention of the actual
problem (no definition of virtual function in base class, nor does the
base class overtly = 0 it):
g++ -Wall virtual.cpp -o virtual
/usr/local/home/eddy/.sys/tmp/ccAtrV2C.o(.gnu.linkonce.t._ZN4BaseC2Ev+0x8): In function `Base::Base[not-in-charge]()':
: undefined reference to `vtable for Base'
/usr/local/home/eddy/.sys/tmp/ccAtrV2C.o(.gnu.linkonce.r._ZTI7Derived+0x8): undefined reference to `typeinfo for Base'
collect2: ld returned 1 exit status
Also present in 3.4.1:
g++-3.4 -Wall virtual.cpp -o virtual
/usr/local/home/eddy/.sys/tmp/ccNDFkO3.o(.gnu.linkonce.t._ZN4BaseC2Ev+0x8): In function `Base::Base()':
: undefined reference to `vtable for Base'
/usr/local/home/eddy/.sys/tmp/ccNDFkO3.o(.gnu.linkonce.r._ZTI7Derived+0x8): undefined reference to `typeinfo for Base'
collect2: ld returned 1 exit status
The complaint directs me to the constructor; I have seen it attribute
this to a line in a header file from a third-party library. I have seen
this (on larger and more complex code) without the message about
typeinfo for Base being absent; only the vtable's absence was reported.
>How-To-Repeat:
When reporting a compiler error, preprocessor output must be included - OK, here it is:
g++ -E virtual.cpp
# 1 "virtual.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "virtual.cpp"
class Base { public: virtual void Method(void) ; };
class Derived : public Base { public: virtual void Method(void) {} };
int main(void)
{
Derived thing;
Base *ptr = &thing;
ptr->Method();
return 0;
}
>Fix:
Reply to: