I have these files: classA.cpp
, classA.h
, classB.cpp
, classB.h
, and main.cpp
.
All needed libraries are included in both .h files.
In main.cpp
I include classA.cpp
and classB.cpp
.
In classB.cpp
I include classB.h
and in classA.cpp
it is classA.h
I compile it by
g++ main.cpp
(+some unimportant stuff) and it is working perfectly.
But I am almost certainly sure, that on our lectures we were told to do that differently, sadly I can't find it now.
Is this the best way of including and compiling? If not, what is?
Normally you don't include source files into other source files. In fact, the reason we have header files is so that we can use the same declarations in all files, whilst compiling definitions only once.
So your
main.cpp
should includeclassA.h
andclassB.h
instead ofclassA.cpp
andclassB.cpp
:Then write a simple Makefile:
For bigger projects, you can write a rule to create the dependencies from the source files automatically, but we can start as above by hand-generating them.
Now, the correct commands will be run when you execute
the simply way:
g++ main.cpp ClassA.cpp ClassB.cpp etc.cpp
more advanced way you should use a makefile. enter link description here