Makefile Example

Multipurpose Makefile

(Updated on 05/31/2017)


What’s not to love about Makefiles? They are intricate, old fashioned, difficult to debug (should something go wrong) and programs like Eclipse completely replace the need for one… So why even bother?

One reason is that you can control every step of your compilation process and another one is its reusability, even if you need to cross compile a project using some specific toolchain. If one is well made, you can save yourself a lot o time jumping around windows or web pages when you need to customize or start a new project.



Attachment Content

The zip file you can download at the end of this post contains:

  1. The project folder, helloworld;
  2. The multipurpose Makefile that serves as the motif for this post;
  3. Source files to build a simple application, in C.

How to Use This Makefile

To make things simpler, I included the Makefile’s usage via help option on the command line that executes it. See below:

The build creates an additional folder, objs, to store the intermediate objects which are combined in the final link. This folder is also used to store the project’s dependency information so it can automatically recompile only what’s needed.

General Overview

In this first code sample I decided to share a Makefile that can be easily modified to do a variety of things, from simply compiling a hello world example (as the attachment does) to managing libraries and big projects.

Here is a list of things this Makefile does:

  • Automatically compiles a program or a library (.a or .so), with or without a version string;
  • Deals with all dependencies so you don’t need to clean whenever you change a header file;
  • Separate options for the compilation process and for the final linkage;
  • It forces one file to always be re-compiled (useful if you need to update the macros __DATE__ and/or __TIME__);
  • Easy way to include or remove debug information from the target and easy way to force a strip call;
  • It has a help option (try make help) and you can toggle the verbosity on if you ever need to see what is being executed;
  • If you need to check some environment variable, it’s prepared to issue an error should the user forgot to export it;
  • Most importantly, it is ready to be used with any toolchain and set of CFLAGS, LDFLAGS, etc…

Needless to say, it’s a big Makefile, but really easy to read and it has a lot of comments to further help understand what each line does.

So, do you work with C projects and need a quick way to create projects, integrate various libraries and deal with release/debug versions of your code, all while cross compiling? Maybe you will find this attachment of use!

A final note: the example compiles a C code, but with small modifications you can adapt it for C++ builds as gcc and g++ are not that different, considering what parameters they accept.

I hope you liked it, have fun!

Final Words

The attached Makefile may be used and modified at your will, except for commercial use.

Don’t forget to leave any questions in the comments, in case you need some help, and good luck!



Download Attachments

Leave a Reply