🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #839

Re: Link error when using templates

#839
From: Raymond Nijssen
Date: Thu, 20 Aug 1992 13:29
96 lines
4624 bytes
In article <1992Aug19.221617.20824@cco.caltech.edu> paulr@hubble.ccsf.caltech.edu (Paul Ray) writes:

>   Here is a program that I wrote to try to use templates.

[... program omitted ...]

I ran into the same problem. Following example shows more clearly what's going
on:


----- file ape.h -------------------------------------------------------

template <class T> class ape
{
public:
          ape();
  char    nut();
};

template <class T>
   ape<T>::ape () {};



----- file ape.cc -------------------------------------------------------

#include "ape.h"

template <class T>
   char ape<T>::nut() {return '*';}



----- file main.cc -------------------------------------------------------
#include <iostream.h>
#include "ape.h"

int main( int argc, char *argv[])
{
   ape<int> chimp;

   cout << chimp.nut();
}

--------------------------------------------------------------------------

The compilation:

 % g++ -v test ape.cc main.cc
 Reading specs from /usr/gnu/lib/gcc-lib/hp720/2.2.2/specs
 gcc version 2.2.2
  /usr/gnu/lib/gcc-lib/hp720/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dhppa -Dhp9000s800 -D__hp9000s800 -Dhp9k8 -DPWB -Dhpux -Dunix -D_HPUX_SOURCE -D__hppa__ -D__hp9000s800__ -D__hp9000s800 -D__hp9k8__ -D__PWB__ -D__hpux__ -D__unix__ -D___HPUX_SOURCE__ -D__hppa -D__hp9000s800 -D__hp9000s800 -D__hp9k8 -D__PWB -D__hpux -D__unix -D___HPUX_SOURCE ape.cc /usr/tmp/cca15984.i
 GNU CPP version 2.2.2 (hp9000s800)
  /usr/gnu/lib/gcc-lib/hp720/2.2.2/cc1plus /usr/tmp/cca15984.i -quiet -dumpbase ape.cc -version -o /usr/tmp/cca15984.s
 GNU C++ version 2.2.2 (hp9000s800) compiled by GNU C version 2.2.2.
  as -o ape.o /usr/tmp/cca15984.s
  /usr/gnu/lib/gcc-lib/hp720/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dhppa -Dhp9000s800 -D__hp9000s800 -Dhp9k8 -DPWB -Dhpux -Dunix -D_HPUX_SOURCE -D__hppa__ -D__hp9000s800__ -D__hp9000s800 -D__hp9k8__ -D__PWB__ -D__hpux__ -D__unix__ -D___HPUX_SOURCE__ -D__hppa -D__hp9000s800 -D__hp9000s800 -D__hp9k8 -D__PWB -D__hpux -D__unix -D___HPUX_SOURCE main.cc /usr/tmp/cca15984.i
 GNU CPP version 2.2.2 (hp9000s800)
  /usr/gnu/lib/gcc-lib/hp720/2.2.2/cc1plus /usr/tmp/cca15984.i -quiet -dumpbase main.cc -version -o /usr/tmp/cca15984.s
 GNU C++ version 2.2.2 (hp9000s800) compiled by GNU C version 2.2.2.
  as -o main.o /usr/tmp/cca15984.s
  /usr/gnu/lib/gcc-lib/hp720/2.2.2/ld -u main -a archive -o test /lib/crt0.o -L/usr/gnu/lib/gcc-lib/hp720/2.2.2 -L/usr/gnu/lib ape.o main.o -lg++ /usr/gnu/lib/gcc-lib/hp720/2.2.2/libgcc.a -lc /usr/gnu/lib/gcc-lib/hp720/2.2.2/libgcc.a
 ld: Unsatisfied symbols:
    nut__t3ape1Zi (code)
 collect: /bin/ld returned 1 exit status


This demonstrates that the call in `main.cc' to the member function 
`ape<T>::nut()' in `ape.cc' cannot be resolved.

However, moving the definition of this function from `ape.cc' to `ape.h' makes
the problem disappear: it compiles, links and runs ok.

I experimented a little to see if I could solve this problem with `#pragma',
but no luck, and I couldn't find any hints in the docs and books on this
topic.

So, the question remains: should a programmer really put all definitions of
each class template in its `.h' file instead of its corresponding `.cc' file? 
This would reduce the benefits of using templates to the source level, and 
it would make all `.cc' files belonging to template class definitions
obsolete, thus largely increasing time-to-compile.

I can hardly imagine that this would be the case without a single word about
it in the C++ ref. manual. So, do I miss something?

All help is greatly appreciated.

aTdhvaannkcse,
-Raymond
--
| Raymond Nijssen         | Eindhoven University of Techology                 |
|                         | Department of Electrical Engineering              |
| raymond@es.ele.tue.nl   | Design Automation Section (ES),  Room EH 7.16     |
| phone: +31-40-473373    | P.O. Box 513, 5600 MB Eindhoven, The Netherlands  |
| fax:   +31-40-464527    | "Trust me. I know what I'm doing" (Sledge Hammer) |
Technical Details
Message-ID: <RAYMOND.92Aug20152900@cobra.es.ele.tue.nl>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!es.ele.tue.NL!raymond
References: <1992Aug19.221617.20824@cco.caltech.edu>

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!es.ele.tue.NL!raymond
From: raymond@es.ele.tue.NL (Raymond Nijssen)
Subject: Re: Link error when using templates
Message-ID: <RAYMOND.92Aug20152900@cobra.es.ele.tue.nl>
Sender: gnulists@ai.mit.edu
Organization: Eindhoven University of Technology, Eindhoven, The Netherlands
References: <1992Aug19.221617.20824@cco.caltech.edu>
Distribution: gnu
Date: Thu, 20 Aug 1992 13:29:00 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 97