This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Static variables in templates


Hello,
  I cannot initialize static variables in templates on HPUX 11. The following 
code compiles and works as you would expect it to on Linux, but fails with a 
linker error in HPUX. The error message follows the program listing. My 
compiler version is gcc-3.1 (built from sources) with binutils-2.12 (built 
from sources).

--- test.cc ---
// Template definition
template<class T>
class foo
{
  public:
    foo(T v) : var(v) { times++; };
    T var;
    static int times;
};

template<class T>
int foo<T>::times = 0;

// Now we use this template.
int main( int , char ** )
{
  foo<int> a(23);
  return 0;
}
---------------

On HPUX, it fails with the following linker error:
---------------
eehp015:~/misc> gcc -o test test.cc
/usr/ccs/bin/ld: Unsatisfied symbols:
   foo<int>::times   (first referenced in /var/tmp//ccikfnVs.o) (data)
collect2: ld returned 1 exit status
---------------
Any pointers would be greatly appreciated. This construct is very important, 
because large parts of KDE 3.x code depend on it. I am trying to port KDE 3.x 
to HPUX with gcc 3.1 release. Please cc any replies/follow-ups to me as I am 
not subscribed to the list. In case this is the wrong place to ask this 
question, I apologize and request you to kindly direct me to the right fora.

Thank you,
Ravi


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]