Complete roguelike tutorial using C++ and libtcod - extra 4: makefiles

From RogueBasin
Revision as of 13:05, 23 October 2015 by Joel Pera (talk | contribs) (pasted →‎top: , cat and sidebar)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Complete roguelike tutorial using C++ and libtcod
-originally written by Jice
Text in this tutorial was released under the Creative Commons Attribution-ShareAlike 3.0 Unported and the GNU Free Documentation License (unversioned, with no invariant sections, front-cover texts, or back-cover texts) on 2015-09-21.


This article will show you how to use a makefile to speed up compilation. If you're using an IDE, you probably don't need this, except if you want to port your application to an OS where your IDE is not available. As with gdb, even if there are better alternatives, it's always useful to know how to quickly setup a makefile for your project.

There are several ways a makefile will speed up compilation compared to recompiling everything every time with a single g++ command line :

  • we will generate an intermediate .o file for each .cpp source file, then link all .o files together to get the executable. When a single cpp file is modified, we only need to recompile its .o and link the new executable.
  • using the same idea, we can generate an intermediate file for a header. This is called a precompiled header and uses the .gch extension. As long as you don't modify a header, the compiler will be able to use this precompiled header instead of re-parsing them.
  • finally, make can use multi-threading to compile several .cpp files at the same time.

Note that with the current size of the project, you won't see much difference in compilation times, but the gain will get bigger and bigger as your project grows.