DEV Community

김세현
김세현

Posted on

Makefile

What is a Makefile?

A Makefile is a special file, traditionally named Makefile or makefile, that contains a set of rules to automate the building, testing, and deployment of projects. It is used with the make utility, a build automation tool that interprets these rules to execute tasks efficiently.


Why Use a Makefile?

  1. Automation: Simplify repetitive tasks like compiling code or running tests.

  2. Dependency Management: Ensure that only the necessary parts of your project are rebuilt, saving time.

  3. Portability: Share build instructions across team members to maintain consistency.

  4. Customizable: Define custom commands for deployment, cleaning, or other project-specific tasks.


Anatomy of a Makefile

A Makefile consists of targets, dependencies, and recipes:

  1. Target

    The target is the name of the task to be executed. It can represent a file to be created or a label for a custom task.

  2. Dependencies

    Dependencies are files or targets that must exist or be up-to-date before the target is executed.

  3. Recipe

    The recipe is the command or set of commands to execute. These commands must be indented with a tab (not spaces).

Basic Syntax:

target: dependencies
<TAB> command
Enter fullscreen mode Exit fullscreen mode

Example

Directory Structure

project/
|-- main.c
|-- utils.c
|-- utils.h
|-- Makefile
Enter fullscreen mode Exit fullscreen mode

Makefile

# Variables
CC = gcc
CFLAGS = -Wall -g

# Targets
all: program

program: main.o utils.o
    $(CC) $(CFLAGS) -o program main.o utils.o

main.o: main.c utils.h
    $(CC) $(CFLAGS) -c main.c

utils.o: utils.c utils.h
    $(CC) $(CFLAGS) -c utils.c

clean:
    rm -f *.o program
Enter fullscreen mode Exit fullscreen mode
  • Variables:

    • CC: Compiler to use (e.g., gcc).
    • CFLAGS: Compiler flags like warnings (-Wall) and debugging (-g).
  • Targets:

    • all: Default target. It depends on program.
    • program: Builds the final executable using main.o and utils.o.
    • main.o and utils.o: Object files compiled from their respective .c files.
    • clean: A custom target to delete generated files.
  • Dependencies:

    • main.o depends on main.c and utils.h, so changes in these files will trigger a recompilation.
  • Commands:

    • Commands like gcc and rm are executed in the shell.

Features of Makefile

  1. Implicit Rules

Make can infer rules, so you don’t always need to specify them explicitly. For example, if you omit the main.o rule, make can deduce how to compile main.c into main.o.

  1. Phony Targets

Use .PHONY to define targets that don’t create files:

.PHONY: clean
clean:
    rm -f *.o program
Enter fullscreen mode Exit fullscreen mode
  1. Wildcards

Automate file selection with wildcards:

SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
Enter fullscreen mode Exit fullscreen mode
  1. Conditional Statements

Add conditional logic:

ifeq ($(DEBUG), 1)
CFLAGS += -DDEBUG
endif
Enter fullscreen mode Exit fullscreen mode

Top comments (0)