This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
explicit template inst. on hpux / optimizer
- To: egcs at egcs dot cygnus dot com
- Subject: explicit template inst. on hpux / optimizer
- From: Markus Werle <markus at lufmech dot rwth-aachen dot de>
- Date: Wed, 23 Jun 1999 15:39:18 +0200
Hi!
In my code example I explicitly instantiate a template which calls template
functions in its constructor, so these are implicitly
instantiated.
Everything works fine on a linuxpc, but the linker fails on hpux.
Since I have to use hp ld on hpux this may be a linker bug from hp-ux 10.20
(B.10.20 A 9000/780) but I am not sure,
so please take a glance at this:
linuxpc > g++ --version
egcs-2.91.66
linuxpc > g++ -c main.C && g++ -c File1.C && g++ -o test main.o File1.o
linuxpc > ./test
We trust in God
Everything above was fine and expected, now:
hpux > g++ --version
egcs-2.91.66
hpux > g++ -c main.C && g++ -c File1.C && g++ -o test main.o File1.o
/usr/ccs/bin/ld: Unsatisfied symbols:
Demo<double>::f(void)(code)
collect2: ld returned 1 exit status
Here is the code:
-----------File1.h--------------
#ifndef FILE_ONE_H
#define FILE_ONE_H
#include <iostream>
template<class C> class Demo {
C garbage;
public: void f(); // I just wanna define it somewhere else };
#endif
-----------File1.C--------------
#include "File1.h"
template<class C> void Demo<C>::f() {
cout << "We trust in God" << endl; }
// this is a special trick to instantiate recursively:
// an explicit instantiation of the constructor instantiates
// implicitly Demo<C>::f()
template<class C> class Instantiator {
Demo<double> D;
public:
Instantiator() { D.f(); }
};
// here we go
template Instantiator<double>;
------------main.C--------------
#include <iostream.h>
#include "File1.h"
int main() {
Demo<double> D;
D.f();
}
========================================
By the way:
The example above also fails to link on linux, when using option -O3.
(not so with -O2 and -O1)
So implicit instantiation through explicit instantiation
is gone away when using optimization. :-(
linuxpc > g++ -c main.C && g++ -c File1.C && g++ -o test main.o File1.o
/usr/ccs/bin/ld: Unsatisfied symbols:
Demo<double>::f(void)(code)
collect2: ld returned 1 exit status
adding explicit instantiation is fine, but not what we wanted:
template void Demo<double>::f(); // class Instantiator obsolete ...
Any compiler-option to force same behaviour with and without
optimization welcome. Thanks for Your help.
Best regards,
Markus