TAGS :Viewed: 11 - Published at: a few seconds ago

[ Makefiles - Inherited classes and inner classes ]

I'm learning c++ and I'm working on a project with +-5 classes I use inheritance and inner classes. I made for every class a separate cpp and hpp file (also for the inner classes) Now i want to make a makefile to compile easier. And i read a lot of tutorial about makefiles, but there are no good tutorials treating the subject inheritance and inner classes.

I will make an example:

class A
class B //is a inner class ob A
class C //inherits from B
class D //inherits from C
class test // containing a main with tests for all the classes

how would i make this in a makefile? (every class has a hpp and a cpp !!!)

you don't need to write a makefile for me, if you have a link to a very good tutorial, I am also happy :)


other question: I read that it's not good to define a inner class in a separate hpp+cpp. Is that true ? What are the risks ?

Thank you for you answers :)

Answer 1


For the makefile, you don't need to worry about the language, you need to worry about the dependencies between files. And the later can be produced by any decent compiler for any decent language (sorry, fortran folks...). With gcc, the necessary arguments are -M -MF dependencyFileName.d. This produces a file containing a rule in Makefile-syntax that can be included into the Makefile to inform make of all the dependencies.

The rule that is generated by the compiler is usually insufficient, it does not contain the dependency file as a target, for instance. You can find a thorough description of the different methods of dealing with this issue here: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/

In general, I would advise against somehow hardcoding the dependencies into the Makefile: Code changes, and so will the dependencies. And an incomplete rebuild does not necessarily produce compiler/linker errors, sometimes you just get really weird behavior of your program. Stuff down the line of:

  • somehow the wrong function is called

  • somehow the arguments get skrewed up during a function call

  • I know that this function must return zero, why do I get a one out of this call?!?

Better take the time to get your automatic dependency generation right before you have spent a week worth of time hunting for such phantoms.