DEV Community

Cover image for Makefile, in practice
Suspir0n
Suspir0n

Posted on

Makefile, in practice

What is Makefile ?

According to the Guia Linux "The make utility is used to check which files need to be compiled (have changed since the last compilation or have never been compiled) and to issue the compilation (and linking) order."

OBS: Rewritten text in English.

The makefile is utilized in sereval programming languages, like: C, C++, C#, Python, JavaScript, TypeScript, PHP between others.

To use, we have some commands:

  • -f: specific the file
  • -e: for variable of file makefile

How to use Makefile:

Alt Text

when starting a command, you have to create a file with a specific nomenclature, make will search your directory for:

  • GNUmakefile
  • makefile
  • Makefile

Write a Makefile

First create a file called "Makefile", how I will utilize python how example, inside him, you add the next:

.PHONY: help
SHELL := /bin/bash

help: ##This help.
    @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help
Enter fullscreen mode Exit fullscreen mode

In it, you are creating the command help, passing the terminal, it goes list all the command of makefile that you can use.

Then create:

run-file: ## run the file main.py
    python main.py
Enter fullscreen mode Exit fullscreen mode

When you run this command in terminal thus make run-file, it will run what have inside of file main.py. Note that have two "#" later of command, it suits to describe the command.

Complete code:

Alt Text

This example demonstrates how to optimize your time and your project is, more robust, reducing commands that you would have to execute among others, solving your problem. This was logbook #05 we are saying goodbye here. We will return with another logbook.

Was this article useful for you?
Leave a comment below.

References

Top comments (0)