Obscure problem with template virtual tables not made weak (?)

Wolfgang Bangerth wolf@gaia.IWR.Uni-Heidelberg.De
Mon Mar 1 03:42:00 GMT 1999


Hi there,

some time ago I ran into a rather obscure problem with virtual
function tables of automatically instantiated templates not being
tagged as "weak". Then, I was able to get around the problem, but now
I hit it again and wasn't, so I put together a simple example.

Consider the following three files:

----------------- a.h --------------
template <int dim> class A {
  public:
    A ();
    virtual void f();
};


------------------ a.cc ------------
#include "a.h"

template <int dim> void A<dim>::f () {};

template class A<2>;


------------------ b.cc ------------
#include "a.h"

template <int dim> A<dim>::A () {};
int main ()                     {};

template class A<2>;
------------------------------------


When I compile this (egcs 1.1b on Linux libc5, but same result using
egcs 1.1.1 on SunOS), using these two commands:

source/test> c++ -c a.cc b.cc
source/test> c++ *.o

I get the following output:

>> b.o(.rodata+0x0): multiple definition of `A<2> virtual table'
>> a.o(.rodata+0x0): first defined here
>> b.o: In function `A<2> type_info function':
>> b.o(.text+0x20): multiple definition of `A<2> type_info function'
>> a.o(.text+0x10): first defined here
>> /usr/bin/ld: Warning: size of symbol `A<2> type_info function' changed from 48 to 44 in b.o
>> collect2: ld returned 1 exit status

The problem may be that in both explicit instantiations the virtual table
is generated but not tagged "weak" and thus the linker is unhappy.


Actually, I am not knowlegable enough to judge whether this qualifies
as a bug or simply is user stupidity. The solution in this case would
be to put all functions of this class into only one file, then it
works; this was what I told my customer the first time. However, this
time it hit me at a place where the compiler choses to instantiate the
virtual function table itself, somewhere where I use that class; I
solved the problem by putting some lines like 
      asm(".weak mangled name of vt")
into that function, but I'd prefer a proper solution. Unfortunately I
don't know egcs so I can only humbly ask...? I'd be happy to
send the whole program if that would help with that (however, I wasn't
able to strip it down, it has ~70.000 lines of code and the problems
appeared when using MI, virtual base classes and templates; the three
files above were the closest I could get to the problem).

Anyway: my respect for creating egcs, thanks guys!

Regards,
  Wolfgang




wave-equation/source> c++ -v
Reading specs from /home/wolf/bin/egcs-1.1b/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)


-------------------------------------------------------------------------
Wolfgang Bangerth            email:       wolf@gaia.iwr.uni-heidelberg.de
                             www: http://gaia.iwr.uni-heidelberg.de/~wolf




More information about the Gcc-bugs mailing list