g++ with -frepo and shared objects...

G'day,

I have been working with a large application that makes extensive use of templates. When compiled under Unix (with g++), this sees some rather impressive bloat. I have been trying to make a temporary quick-fix by using the -frepo option, which results in dramatically smaller shared objects, but the executables no longer link. I suspect that collect2 is not being called when the -shared flag is used, which would be the cause of this. Here is a simple example I created:

template.h:

template <class T>
T tmax(T a, T b)
{
  return a > b ? a : b ;
}

test.h:

int calltmaxint(int i, int j);

test.cpp:

#include "template.h"
#include "test.h"

int calltmaxint(int i, int j)
{
  return tmax(i, j);
}

main.cpp:

#include <iostream>
using namespace std ;
#include "test.h"

int main(void)
{
  cout << calltmaxint(10, 15) << endl ;

  return 0;
}

The command line is then as follows:

$ g++ -c -frepo -fPIC test.cpp
$ g++ -shared test.o -o libtest.so
$ g++ -c -frepo main.cpp
$ g++ -L. -ltest main.o -o main
./libtest.so: undefined reference to `int tmax<int>(int, int)'
collect2: ld returned 1 exit status

I have attempted on Solaris 10 with g++ 3.6.4 and Debian stable with g++ 4.1.1-15 with the same result. I have not been able to find any answers with Google on this, so I am rather hoping someone here can enlighten me. Can I use -frepo with shared objects? If so, what am I doing wrong? If not, will I be able to use the -fexternal-templates option to reduce the size?