Make Command: A Magical Way for Automating Stuff

In this mini-tutorial, you will learn how to write a Makefile for automating stuff from project builds to whatnot, which is executed by the make command in Linux

Why? πŸ€”

Cause it's used in many many projects for building stuff cause when you have to compile thousands of files for a project and ensure that each file is older than the compiled file, then Makefile saves the day!

Why? (Explained) πŸ€”βœ¨

Imagine you have a project which has tons of files dependent on one or another and they need to be compiled at once, compiling them manually is gonna take way too much time!

Also, if you are compiling a file that is already compiled from the latest version of the source file, then you would be compiling it again (what a waste of time and resources!).

You can write a script file for automating the whole process but for ensuring the dependencies are compiled properly, it's gonna get real complicated real soon.

*Le make command: "Hold my Beer" 🍺

make uses a Makefile for automating the whole process of building stuff from start to finish.

If dependencies are files, it will ensure they are updated and if they are already updated according to the source code, it won't update them (Saving time and resources!).

Also, you can individually build each dependency with make if you want to.

Structure of Makefile

target: dependency
    action

Details πŸ—’

  1. target: target file or command that will be invoked when a user executes the make command with following Command-Line Argument make target

  2. dependency (optional): they can be as many as you want (space separated) and they are a dependency for building that specific target

  3. action: that's where you type the commands that need to be executed for building that target

Note:

  1. If a user simply executes make without any option, the target named all will be executed.

  2. Before typing the command for building the target in action, ensure to add a tab instead of spaces or it will keep saying ":makefile:4: *** missing separator. Stop." or something like that.

  3. Dependencies can be files as well in the local directory.

Example 1:

Save the following code in a file and name it Makefile

all: one two
        echo "Executed all"

one:
        echo "Building one"

two:
        echo "Building two (part 1)"
        echo "Building two (part 2)"

I kept it simple so we can understand how make works...

Now execute the make command like this:

make

Output:

echo "Building one"
Building one
echo "Building two (Part 1)"
Building two (Part 1)
echo "Building two (Part 2)"
Building two (Part 2)
echo "Executed all"
Executed all

Breaking down how it executes

  1. It Builds dependencies first if don't exist
  2. It Builds the target when dependencies exist

How the above Makefile executes

(using make all or make command)

It checks if dependency one and two exist, if they don't exist (or is outdated), They become a target and their dependencies are checked, if they don't have any, then the commands (actions) are executed for building them.

Then finally, the all target is built using those dependencies.

Example 2 (with same Makefile)

If for some reason, you wanna build the individual dependencies, simply specify the target while executing the make command.

Say you wanna build one

make one

Output:

echo "Building one"
Building one

If it had any dependency, it would have built before this.

Same goes for building two

make two

Output:

echo "Building two (Part 1)"
Building two (Part 1)
echo "Building two (Part 2)"
Building two (Part 2)

End of Article

Now you know a little bit about how you can use the make command in Linux for automating stuff from builds to what not.

Optional

If you find this article useful, then leave a like and support me by following me on Twitter.

Thanks for reading πŸ˜ƒ

29