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

Re: C++ / g++ question

> This is a little OT but I have a question for any C++ people who might be
> listening:
> I have a program that uses two classes : a matrix class and a vector
> class (ie that I wrote). I use templates to implement this so that my
> vectors/matrices can be use several data types ( eg int, float, double )
> as their fields.
> I have it split up like this:
> vector.h
> matrix.h
> vector.cpp
> matrix.cpp
> main.cpp
> g++ -o matrix vector.cpp matrix.cpp main.cpp 

Quite simply the compiler is producing three nice little .o's and
handling only one of them to the linker.

The following would work:

g++ -c *.cpp
g++ -o matrix vector.o matrix.o main.o

> 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 

Here all the cpp files are mixed by the precompiler into a big one, so
there all the functions are in the intermediate .o created by the

> works as advertised
> AFAIK, #including .cpp files is not "standard practice". Or is it ? Am I
> missing something ?

It is legal but considered bad practice for readability/mantainability

			Jean Francois Martinez

Project Independence: Linux for the Masses