delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/01/20/18:06:35

From: Jason Green <news AT jgreen4 DOT fsnet DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Odp: Odp: Library with templates - help !!!!!!!!!!!!!
Date: Sat, 20 Jan 2001 22:57:44 +0000
Organization: Customer of Energis Squared
Lines: 60
Message-ID: <eimj6tkicge2k0ivsced7fbcgnhg968c63@4ax.com>
References: <94c3au$791$1 AT info DOT cyf-kr DOT edu DOT pl> <ul8j6tofuvdpb22ms23d9ld99sr7hk3k25 AT 4ax DOT com> <94cba9$5a0$1 AT info DOT cyf-kr DOT edu DOT pl> <t6jf5hsbjr8k83 AT corp DOT supernews DOT co DOT uk> <94cjum$17k$1 AT info DOT cyf-kr DOT edu DOT pl>
NNTP-Posting-Host: modem-159.helium.dialup.pol.co.uk
Mime-Version: 1.0
X-Trace: news7.svr.pol.co.uk 980031473 21050 62.136.1.159 (20 Jan 2001 22:57:53 GMT)
NNTP-Posting-Date: 20 Jan 2001 22:57:53 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Agent 1.7/32.534
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Rafal Maj" <maj_rafal AT poczta DOT onet DOT pl> wrote:

> > > When I write definition of normal class in my library *.cc file - OK
> > > When I write definition of  template class in my main *.cc file - OK
> > > When I write definition of  template class in my library *.cc file -
> error !
> >
> > That's just the way template classes work, I'm afraid.
> > (So far as I know anyway.)

Yep, this is how it's supposed to be.

> > Some compilers can "compile the headers" on order to answer
> > your objection to writing the code correctly, but I don't
> > think gcc does so yet.

AIUI, any compiler that does this without use of the 'export' keyword
does so in a non-standard way.

> But when I add template to cBox :
> lib.h :  template <class typ> class cBox { public : void move(); }
> lib.cc :   template <class typ> void cBox<typ>::move() {    /*
> ..............very long algoritm............ */  }
> main.c :   #include "lib.h"   main() { cBox obj<int>;  obj.move();  }
> 
> I get error while building (not when compiling) :

Correct, it's a _linking_ error, and it is correct behaviour.  In
fact, the linker would be wrong *not* to issue an error here.

> When I copy-paste source of lib.cc / lib.h into main.cc everything is O.K.,
> but any small change to main() function makes class cBox recompile and
> vice-versa. In my program, when it is divided to several files it compile +
> build after changing one library in about 5sec,  when this program was in
> one file it took about 20s to re-compile after every change.

That's an unfortunate side-effect of templates.  I expect the gcc
developers have higher priorities at the moment than to figure out how
to implement 'export', I don't know if any of the commercial compilers
support this yet but I doubt it.

> I read in Rhide help about CXX compile flags, that some flags (like -frepo)
> are needed for such programs 

This isn't one such program, it's a typical use of template classes,
and a common misunderstanding of how they work.

>  (or maybe I didn't understand docs... my english is bad). 

Actually, your English is very good, if you are saying it isn't your
first language - I can't tell.  As far as the docs go, you really need
a good book to explain this (and other non-obvious features of C++).

> So what compile/link flags I should use to make this library (.cc + .h) work
> while using template classes in it ???

You can't fix this by using some special compiler/linker flags.  There
are some tricks to produce behaviour more like a non-template library.
I won't attempt to explain them here, you'll get a better answer if
you ask in comp.lang.c.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019