[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ / g++ question



> g++ -o matrix vector.cpp matrix.cpp main.cpp 
> 
> bombs with error messages from ld (no errors from the compiler). However,
> if I put 
> 
> #include "vector.cpp"
> #include "matrix.cpp
> 
> in the top of main.cpp, then 
> 
> g++ -o matrix main.cpp 
> 
> works as advertised
> 
> AFAIK, #including .cpp files is not "standard practice". Or is it ? Am I
> missing something ?
> 
> CHeers,
> -- 
> Donovan
Actually, there are a few different "standard practices" when it comes to
templates, and none of them particularly elegant (unless egcs has finally
come up with one, I admit I'm still back on 1.0.3a)

One standard practice is to put definitions of template member functions in 
header files, so there is no vector.cpp and matrix.cpp, it's all in vector.h
and matrix.h. An alternative gnu-ish practice is to use "#pragma interface";
see an example of this in <std/complext.h>. (I forget the details, but I 
remember I wasn't too crazy about this solution either...)

So... you're not doing anything wrong, it's just that current compilers find
it difficult to deal with templates being split off into another file, since
they can't simply turn it into normal object code without knowing what
the actual types of the variables shall be.

_____---------------------------------------------------------------------_____
                              S. Lockwood
                         sjl@zorbas.ece.ucsb.edu
                    http://folletts.ece.ucsb.edu/~sjl
_____---------------------------------------------------------------------_____