Multi-File Program Linking

This question, I probably should have asked long ago, but I am thinking I am doing this linking of files all wrong.

I have a base class, and its .h & .cpp
then two sub-classes, also with their two files.

Usually, I link to the .h file from .cpp.
When I have sub-classes I am unsure how to link all of the classes to the Base Class, and then to the main.

Do I link to the Header of the Base Class from both my sub-class Headers?

Should I link to the Base .cpp from my main only ?

I am just lost at this point,

I hope somebody can clear this up for me.
I thank you in advance ...
Bob
Last edited on
Let's say you have
base.cpp and base.h

Also two derived classes, both of which have #include "base.h"
d1.cpp and d1.h
d2.cpp and d2.h


And finally a main.cpp which has #include "d1.h" and #include "d2.h"

Compiling is simply
g++ base.cpp d1.cpp d2.cpp main.cpp


I use code blocks.
Right, so create a console project, and add all your source and header files to the project.
Ok, but generally I do all my own files (no project) then just build. Is that not a good way of doing it?
I just feel somehow I am not linking properly, and getting some errors that take some time to figure out - but I'm thinking some those errors are all because of poor linking.

I am thinking : I should do like I have been - - #include Header File in the cpp
and in main #include all my .cpp files


I guess that is what you were saying above huh?
> and in main #include all my .cpp files
> I guess that is what you were saying above huh?
No that is not what I'm saying at all.

Including one .cpp inside another .cpp is the wrong way to do it.

For small projects, this works fine.
g++ base.cpp d1.cpp d2.cpp main.cpp

For larger projects, you use make (or whatever your IDE calls a project).
Regardless, the automation usually ends up doing.
g++ -c base.cpp
g++ -c d1.cpp
g++ -c d2.cpp
g++ -c main.cpp
g++ base.o d1.o d2.o main.o


The point of this being that tools like make minimise the amount of recompilation.
If you've got 100's or 1000's of source files, you don't want to be recompiling everything for every small edit you make.
Topic archived. No new replies allowed.