🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #50052

g++ and templates (FAQ?)

#50052
From: Alexander P. Zijdenbos
Date: Fri, 19 Nov 1993 05:23
96 lines
3068 bytes
Hello,

I'm having problems using gcc/libg++ with template classes. I recently
installed gcc-2.5.2 and libg++-2.5.1 on a Sun machine running Solaris
2.2, and as far as I can tell I was successful in doing that.
Compilation of the code I have works fine, but at link-time I get
"Undefined symbol" errors related to the template class.

I've been staring at the bit about -fexternal-templates and the
#pragma directives, but so far I haven't been able to grasp how all
that works in practice, where to place what. I hope somebody is able
to give me an example, or tell me what I'm doing wrong...

Some sample code:

****************************************************************
Array.h (Basically the template Array class from the C++ Primer)
****************************************************************

#ifndef ARRAY_H
#define ARRAY_H

#define DEFAULT_SIZE 32

template <class Type>
class Array
{
// data members
  int   size;
  Type *contents;

public:
// manager functions
  Array (int sz = DEFAULT_SIZE);
  Array (Type *, int);
  Array (const Array&);
  ~Array ();
....

*****************************
Array.c 
*****************************

#include "Array.h"

template <class Type>
Array<Type>::Array (int sz)
{
   // allocate an integer array of size
   contents = new Type[size = sz];

   // initialize array
   for (register int i = 0; i < size; i++)
     contents [i] = 0;
}
....

******************************
test.c
******************************

#include "Array.h"

main(int    argc, 
     char **argv)
{
  Array<int> testArray(6);
}

****************************

When I `make' this piece of code, I get:

****************************

make -f testMake
g++ -g   -c   Array.c
g++ -g   -c   test.c
g++ -g      -o test Array.o  test.o
Undefined			first referenced
 symbol  			    in file
_._t5Array1Zi                       test.o
__t5Array1Zii                       test.o
ld: fatal: Symbol referencing errors. No output written to test
*** Error code 1
make: Fatal error: Command failed for target `test'

****************************

How do I get this to work? Where do I place what #pragma directives,
do I have to compile with -fexternal-templates? There's something
about typedefs in the gcc manual page - how does that come into play?

Thanks a lot,

-- Alex
Technical Details
Message-ID: <1993Nov19.052342.10027@news.vanderbilt.edu>
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!howland.reston.ans.net!wupost!udel!darwin.sura.net!news.Vanderbilt.Edu!alex

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,347d45743de90fff,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1993-11-18 22:50:12 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!howland.reston.ans.net!wupost!udel!darwin.sura.net!news.Vanderbilt.Edu!alex
From: alex@vuse.vanderbilt.edu (Alexander P. Zijdenbos)
Subject: g++ and templates (FAQ?)
Message-ID: <1993Nov19.052342.10027@news.vanderbilt.edu>
Originator: alex@taacman
Sender: news@news.vanderbilt.edu
Nntp-Posting-Host: taacman.vuse.vanderbilt.edu
Organization: Vanderbilt University School of Engineering, Nashville, TN, USA
Date: Fri, 19 Nov 1993 05:23:42 GMT
Lines: 96