Makefile Explained (Simple Guide)

This article introduces Makefile, including its advantages, implementation details, and related information to help improve efficiency when using Makefile.

Jul 24, 2022 · 1 min read · 173 Words · -Views -Comments · Programming
Build Tools 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

While examining projects, I noticed that some projects, especially backend projects, have a Makefile file, which allows convenient encapsulation and calling of build commands.

I hadn’t learned about this tool before, so I’m documenting it here.

Features

  1. Native support on Mac/Linux - once declared and created, you can execute it directly
    • For shell files, you still need to manually add execute permissions chmod +x *.sh
    • Using Node.js to implement build tools in projects is also possible, but there are still environmental dependencies
  2. Supports variable parameter passing

Example

# Build frontend image make build version=1.0.0
build:
	docker build -t stacker/web:v$(version) .

For example, encapsulating the above command into a Makefile allows you to simply run make build version=1.0.0 whenever you want to package a new image file

Syntax Notes

  • Recommended filename is Makefile, but makefile is also acceptable
  • Only supports single-line comments
  • Commands starting with @ indicate that the command output should not be printed

Common Errors

Makefile:5: *** missing separator. Stop.

Commands must be indented with tabs, not spaces

https://static.1991421.cn/2022/2022-11-29-000144.jpeg

Reference Configurations

  1. https://gitlab.com/gnachman/iterm2/-/blob/master/Makefile
  2. https://github.com/jcsalterego/pngpaste/blob/master/Makefile
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover