Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 3 Apr 1999 16:20:45 -0600 (CST)
From:      Nikita Sawant <nikita@evl.uic.edu>
To:        Benjamin George <BenG@thekeyboard.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: gcc (g++) 2.7 and templates?
Message-ID:  <Pine.SGI.3.95.990403161320.9673B-100000@laurel.evl.uic.edu>
In-Reply-To: <37062301.5ED2D081@thekeyboard.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

I had similar problems with compiling C++ programs with g++

Hope this helps .. Nikita

================================================================================

Problem with Templates
-----------------------

http://www.appcomp.utas.edu.au/documentation/gnu-info/g++FAQ.html#SEC33

This Link has some info. on problems with g++ and templates.

NOTE : g++ does not implement a separate pass to instantiate template functions
       and classes at this point; for this reason, it will not work, for
       the most part, to declare your template functions in one file and define them in
       another. The compiler will need to see the entire definition of the 
       function, and will generate a static copy of the function in
       each file in which it is used. 


Solution 1:
----------
Put the definitions in the same file as the template declaration

Solution 2:
----------
g++ does not automatically instantiate templates defined in other files. Because
of this, code written for cfront will often produce
undefined symbol errors when compiled with g++. You need to tell g++ which
template instances you want, by explicitly instantiating
them in the file where they are defined. For instance, given the files 

`templates.h': 

template <class T>
class A {
public:
  void f ();
  T t;
};

template <class T> void g (T a);

`templates.cc': 

#include "templates.h"

template <class T>
void A<T>::f () { }

template <class T>
void g (T a) { }

main.cc: 

#include "templates.h"

main ()
{
  A<int> a;
  a.f ();
  g (a);
}

compiling everything with g++ main.cc templates.cc will result in undefined
symbol errors for `A<int>::f ()' and `g
(A<int>)'. To fix these errors, add the lines 

template class A<int>;
template void g (A<int>);

to the bottom of `templates.cc' and recompile. 



:)

On Sat, 3 Apr 1999, Benjamin George wrote:

> I'm trying to compile a C++ program that uses templates, and I keep on
> getting errors, but I can't figure find anything wrong with the code.
> Is there a problem with gcc 2.7 and templates?  I'm compliling it with
> this command: g++ -o progname main.o token.o stack.o (after compiling
> the three ".o" files using "g++ -c main.cpp", etc).  The error I get is:
> "in main.o: undefined symbol 'stack<int> :: push(int)' in text segment".
> 
> Any ideas?
> 
> Thanks,
> Toby
> 
> P.S.  I tried to send this before, but I don't think it got sent.  Sorry
> if you get it twice.
> 
> --
> My Waterskiing Pages (Barefoot & Wakeboarding):
>     http://waterski.pharamond.com
> 
> 
> 
> 
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message
> 



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SGI.3.95.990403161320.9673B-100000>