Makefiles :)

Sahil UpasaneSahil Upasane
1 min read

Quick and Easy Makefile Guide

Begin by crafting a file named Makefile – the control center for your project's build rules.

In your Makefile, set up a rule like this:

target: dependencies
    command
  • target: Your desired output, like an executable.
  • dependencies: Files needed for building.
  • command: The action to build from dependencies.

For a straightforward C program, a rule might look like:

app: main.c utils.c
    gcc main.c utils.c -o app

This rule compiles main.c and utils.c into an executable named app.

Navigate to your terminal, hop into the Makefile's directory, and type:

make

Watch the magic unfold as Make executes your rules, compiling your program seamlessly.

Make automagically tracks file dependencies. Change a source file, and only the necessary parts get recompiled on the next make run.

Incorporate variables for flexibility:

CC=gcc
app: main.c utils.c
    $(CC) main.c utils.c -o app

Now, altering compilers is a breeze – just tweak the CC variable.

As your project evolves, dig into advanced features like functions, automatic variables, and built-in rules.

Makefiles offer a practical way to automate your build process. This guide gets you started smoothly. As you dive deeper, explore extra features to fine-tune and customize your project's build flow!

1
Subscribe to my newsletter

Read articles from Sahil Upasane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sahil Upasane
Sahil Upasane