From owner-freebsd-questions Sat Apr 3 14:22:50 1999 Delivered-To: freebsd-questions@freebsd.org Received: from evl.uic.edu (evl.evl.uic.edu [131.193.48.80]) by hub.freebsd.org (Postfix) with SMTP id BB29614CA3 for ; Sat, 3 Apr 1999 14:22:45 -0800 (PST) (envelope-from nikita@evl.uic.edu) Received: from laurel.evl.uic.edu (laurel.evl.uic.edu [131.193.48.164]) by evl.uic.edu (950413.SGI.8.6.12/950213.SGI.AUTOCF) via ESMTP id QAA09528; Sat, 3 Apr 1999 16:20:45 -0600 Received: from localhost (nikita@localhost) by laurel.evl.uic.edu (8.8.7/8.6.4) with SMTP id QAA09243; Sat, 3 Apr 1999 16:20:45 -0600 (CST) X-Authentication-Warning: laurel.evl.uic.edu: nikita owned process doing -bs Date: Sat, 3 Apr 1999 16:20:45 -0600 (CST) From: Nikita Sawant To: Benjamin George Cc: freebsd-questions@freebsd.org Subject: Re: gcc (g++) 2.7 and templates? In-Reply-To: <37062301.5ED2D081@thekeyboard.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 A { public: void f (); T t; }; template void g (T a); `templates.cc': #include "templates.h" template void A::f () { } template void g (T a) { } main.cc: #include "templates.h" main () { A a; a.f (); g (a); } compiling everything with g++ main.cc templates.cc will result in undefined symbol errors for `A::f ()' and `g (A)'. To fix these errors, add the lines template class A; template void g (A); 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 :: 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