This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
No definition of virtual function, omits inline and virtual table
- To: gcc-bugs at gcc dot gnu dot org
- Subject: No definition of virtual function, omits inline and virtual table
- From: Jim Carter <jimc at math dot ucla dot edu>
- Date: Tue, 12 Sep 2000 15:15:38 -0700 (PDT)
/* bug03.C -- No definition of virtual function, omits inline and virtual table
When a class has a virtual function not specifying =0 (abstract), but in the
compilation unit that should have emitted the virtual table, inline bodies,
etc. there is no definition of the virtual function, then the inline-type
material is not emitted.
This demo program only fails to emit the inline constructor and the type_info
material, but in a more complicated example the virtual table itself was also
undefined at link time. In truth, in the production code =0 was left off by
mistake, but it should be possible to define the virtual function in an
entirely separate compilation unit, and the virtual table should have an
external reference to it that can find it wherever it may be (if existing).
Version: gcc v2.95.2
Configure: --prefix=/usr/local --enable-shared \
--enable-languages=c++,f77,java
Environment: i686-pc-linux-gnu
(Slackware 7.1, kernel 2.2.16, Intel Pentium 3)
Command line: g++ -frepo -g -c -o bug03.o bug03.C
g++ -frepo -g -o bug03.exe bug03.o
Submitted-date: 2000-09-12
To: gcc-bugs@gcc.gnu.org
Submitter: James F. Carter <jimc@math.ucla.edu>
James F. Carter Voice 310 825 2897 FAX 310 206 6673
UCLA-Mathnet; 6115 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA 90095-1555
Internet: jimc@math.ucla.edu (finger for PGP key)
UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc
Output complained about:
bug03.o: In function `U::U(int)':
/home/jimc/misc/gcc-bugs/Vorotree/bug03.C:48: undefined reference to `T::T(int)'
bug03.o: In function `U type_info function':
/home/jimc/misc/gcc-bugs/Vorotree/bug03.C(.gnu.linkonce.t.__tf1U+0x10): undefined reference to `T type_info function'
bug03.o: In function `main':
/home/jimc/misc/gcc-bugs/Vorotree/bug03.C:57: undefined reference to `T type_info node'
collect2: ld returned 1 exit status
*/
#include <iostream.h>
struct T {
int content;
T(int c) : content(c) { }
virtual int twiddle() /*= 0*/; //Everything's fine if you include =0
};
/*int T::twiddle() { return content; }*/ //Also works if you define the fcn
struct U : public T {
U(int c) : T(c) { }
virtual int twiddle();
};
int U::twiddle() {
return content*content;
}
int main() {
U u(3);
cout << "u = " << u.twiddle() << '\n'; //Should print "u = 9"
return 0;
}