DEV Community

Cover image for What is a Makefile and how do I use them
Alex Merced
Alex Merced

Posted on

What is a Makefile and how do I use them

The beauty of scripting and automation

Today is a great world to live in when it comes to scripting and automating tasks in your computing environment. Out of the box of most Unix-based systems you have several tools to write scripts.

  • The Bash command line which has its own scripting language
  • Perl, while maybe not the first choice for a full-blown application, is a very powerful language for command-line scripting
  • Many Unix based systems may already come with some version of Ruby, Python, PHP which can also be used for scripting

Plus so many other fun scripting languages we can add to our system:

  • Raku (Formerly Perl 6)
  • NodeJS to write scripts in Javascript

With all these great options, I want to talk about Makefiles. Make is a utility on the Unix system for automating a series of terminal commands. These were typically used to help automate the compilation of software which in Languages like C and C++ can require a more complex chain of commands to link dependencies and source code. Although typically used for software compilation can be used to automate all sorts of things.

The Syntax

To use Make you start by creating a file called Makefile (case sensitive)

    npm init -y
    npm install express morgan dotenv
    touch server.js
    mkdir views models views

    git add .
    git commit -m "ready to publish"
    git push origin master
    npm publish
Enter fullscreen mode Exit fullscreen mode

With the above make file we can run either of the chain of terminal commands like so.

make express

make publish

What if for some reason you wanted to use a different filename other than Makefile, this is doable. Take the following file.


    mkdir website
    touch website/index.html
    code website
Enter fullscreen mode Exit fullscreen mode

You can then run this file with the following command.

make website --makefile=MakeMore


Hopefully, this helps you understand what Makefiles are how they work, they can be a pretty nifty tool.

Top comments (1)

gypsydave5 profile image
David Wickes

Nice article - a small correction: Makefile isn't case sensitive - you can also just have a Makefile called... well, makefile.

Also, you failed to mention "one of the worst design botches in the history of Unix", in the words of Eric S. Raymond - namely that each of the command lines needs to be indented with a tab character and only a tab character - no spaces allowed!

Why, you ask?

Why the tab in column 1? Yacc was new, Lex was brand new. I hadn't tried either, so I figured this would be a good excuse to learn. After getting myself snarled up with my first stab at Lex, I just did something simple with the pattern newline-tab. It worked, it stayed. And then a few weeks later I had a user population of about a dozen, most of them friends, and I didn't want to screw up my embedded base. The rest, sadly, is history.

-- Stuart Feldman, who wrote make.

A salutory lesson in getting your public API right the before it becomes too public!