Mail Archives: djgpp/2001/01/20/18:06:35
"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 -