Build a Simple Makefile

TL;DR: If you don’t have anything better to do, create Makefiles by hand :) Gist here.

The other day I went back to origins with building a small C++ project. With modern tools, all went OK. However, deployment was a bit of a problem, in the sense the host had an obsolete toolchain:

Me: GCC 5.2.x, CMAKE 3.3 …
Host: GCC 4.7.x, no CMAKE :(

So, I had to build a makefile by hand!

Following some best practices, I’ve devised a simple Makefile with following sections:

  1. A declaration section, where all parameters are specified
  2. A targets section, where different targets are specified (e.g. compilation, linking…)

The declaration section

Here I’m basically making the script flexible. I’m declaring:

  • The compiler
  • The compiler flags
  • The sources and objects
  • The project (binary) name

The section looks like this:

The targets

The targets sections is as simple as possible:

  • an “all” target to build the MAIN
  • The compilation target (from C++ to .o)
  • The linking target
  • A cleanup target (if you get to copy files around like me)

It looks like this:

Please not that make is picky and you need tab indenting for targets.


This is a simple Makefile to build an executable. Generally, you’ll need to edit the CXX , CXXFLAGS , INCLUDE , SRC and MAIN. Alternatively you can use a modern tool like CMAKE, which will create an OS-dependent makefile.

Best part is the CMAKE equivalent is much simpler:


A little experiment: If you find this post and ad below useful, please check the ad out :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top