Mail Archives: djgpp/2001/01/22/22:07:55
Sorry, maybe I don't understand way of writing C++ libraries...
but I think this is correct :
In file lib.h I write :
class cBox { public : void move(); }
In lib.cc :
void cBox::move() { /* ..............very long algoritm............
/ }
In main.cc :
#include "lib.h" main() { cBox obj; obj.move(); }
In project file :
- add "main.c"
- add "lib.cc"
Now everything is correct, when I'm doing change in main.cc only main.cc is
re-compiling, same fore lib.cc
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) :
Compiling: main.cc
no errors
Creating: main.exe
Error: main.o: In function `main':
main.cc(2) Error: undefined reference to `box<int>::move()'
There were some errors
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.
I read in Rhide help about CXX compile flags, that some flags (like -frepo)
are needed for such programs (or maybe I didn't understand docs... my
english is bad). When I simply select compile option -frepo it didn't help.
So what compile/link flags I should use to make this library (.cc + .h) work
while using template classes in it ???
Please help...
Użytkownik Ian Miller <itm AT shelob DOT plus DOT com> w wiadomooci do grup
dyskusyjnych napisał:t6jf5hsbjr8k83 AT corp DOT supernews DOT co DOT uk...
> "Rafal Maj" <maj_rafal AT poczta DOT onet DOT pl> writes:
>
> > Yes,
> > but when definiton (in real program - very long) of my class is in
box.h,
> > then it is re-compiled every time, with can take very much time.
>
> What Jason Green showed you is the normal way of writing template
> classes and their functions...
>
> > I want to
> > have definition in other .cc file to re-compile it only when necessary,
in
> > bug.h I want only definition of my class.
>
> ...and this isn't. (I believe you mean "declaration" by the way.)
>
> > 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.)
>
> 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.
>
> Regards,
> Ian
- Raw text -