I have some template classes I’ve written that are dependencies for several other classes I need to compile. I have a few options as to how I can do this in my Makefile:
-
Each class that requires a template lists its template requirements. This has the drawback of needing to recreate the dependency tree every time I want to add a new class.
-
Make the templates into target somehow. Either by compiling them, or by making some shadow dependency tree that does not compile anything, but simply forces a recompilation in the event of one of the templates becoming modified.
Any suggestions are welcome.
As Neil Butterworth mentioned in a comment, make deals with files. Say you have a
foo.cppand abar.h. The latter contains your template. And the former might do, for example:While your
Fooclass inherits and thus depends on yourBartemplate class, you are also already declaring your file dependencies with your#includes.It’s those same file dependencies that you specify in your Makefile:
For this rule, make expects that the commands you specify create
foo.o, based on thefoo.cppandbar.hfiles. If you’ve already builtfoo.oonce, and those dependencies haven’t changed, make knows it can skip the rule altogether.It may seem tedious to specify file dependencies twice, but luckily dependencies in your Makefile can be automatically generated from
#includes in your source code by GCC using the-Mcommand-line parameter. You can read about this in thegcc(1)manpage. Simply take GCC’s output, save it somewhere, typicallyfoo.d, andincludethat in your Makefile.With some wizardry, you can automate the generation of
*.ddependency files in the same Makefile that builds your application.