Quick post on Make tool

Make Tool

Introduction

If you've ever installed software for a Unix-based system by compiling from source, you would have run commands such as 'make compile'. Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles.

Let's look at how it works.

Simple Example

Fig. 1 - Basic Example Code Snippet

Here are the key points:
  1. We start by creating a 'Makefile' in the directory. 
  2. A Makefile has recipes that use the prerequisites to make the target.
target: prerequisites
<TAB> recipe

We can run the 'makefile' by running the 'make' command at the command line. The make command runs the default target which is also called the 'default goal'.

The prerequisites can be files or just a name for recipes, in which case they are called "phony targets".

A few more points to keep in mind:
  1. As we can see from the example in fig. 1, the recipe, 'echo "Hello, World!"' is included in the output, we can use the '@' operator to prevent that.
  2. Normally, if we have multiple targets, only the first target is executed as the default. We can change it by including the '.DEFAULT_GOAL := <target>' on the first line of code.
  3. The other way to override this behavior and run all targets is to include 'all: <name of all targets>' on the first line.
  4. We can set variables by using the ':=' operator (e.g. CC = gcc) and to use it use the ${} operators (e.g. ${CC} -c make). ':=' is also called as the simply expanded variable.

Full example

Fig. 2 - Full Example Code Snippet

The above code snippet expands to the following.

Fig. 3 - Full Example Code Snippet

Thank you for reading!

---

Sources:

[1] https://opensource.com/article/18/8/what-how-makefile

[2] https://www.gnu.org/software/make/

Comments

Popular posts from this blog

Playing around with Dell R520 server

Experience Interviewing for an Infrastructure Engineer - Continuous Delivery position

2023 Summer Reading List Summary