"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Understanding the Makefile (Example with C language).

Understanding the Makefile (Example with C language).

Published on 2024-07-31
Browse:858

Comprendre le Makefile (Exemple avec langage C).

A Makefile is a file used by the make tool to automate the compilation of programs. Here are the standard rules and best practices for writing an effective Makefile:

Basic Structure of a Makefile

  1. Target: What you want to build (e.g. an executable file).
  2. Prerequisites: The files necessary to build the target (e.g. source files).
  3. Rule: The command to execute to create the target.

Simple Example

target: prerequisites
    command

Standard Rules

  1. Default rule: The first target in the Makefile is the one that will be built by default.

  2. Compiling source files:

    • Use variables for compilers and options.
    • Example :
   CC = gcc
   CFLAGS = -Wall -g
   SOURCES = main.c utils.c
   OBJECTS = $(SOURCES:.c=.o)
   TARGET = mon_programme

   $(TARGET): $(OBJECTS)
       $(CC) -o $@ $^

   %.o: %.c
       $(CC) $(CFLAGS) -c $



  1. Phonies: Uses .PHONY for targets that are not files.
   .PHONY: clean
   clean:
       rm -f $(OBJECTS) $(TARGET)
  1. Variables: Uses variables to simplify the management of paths and options.
   CC = gcc
   CFLAGS = -Wall
  1. Dependency management: Uses implicit rules and patterns to reduce repetition.

  2. Automatic dependencies: You can generate dependencies automatically for .o files.

   -include $(OBJECTS:.o=.d)

Full Example

Here is a complete Makefile example:

# Variables
CC = gcc
CFLAGS = -Wall -g
SOURCES = main.c utils.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = mon_programme

# Règle par défaut
all: $(TARGET)

# Lien de l'exécutable
# $@ -> $(TARGET)
# $^ -> $(OBJECTS)
$(TARGET): $(OBJECTS)
    $(CC) -o $@ $^

# Compilation des fichiers .c en .o
# $ Premier element des pr
%.o: %.c
    $(CC) $(CFLAGS) -c $



Good practices

  1. Indent with tabs: Commands in rules must be indented with tabs, not spaces.

  2. Comment code: Uses comments to explain sections of the Makefile.

  3. Group files: If your project contains multiple files, organize them into subdirectories and use variables to manage the paths.

  4. Use implicit rules: Take advantage of make's built-in rules to avoid rewriting common rules.

Why use .PHONY?

  • Avoid conflicts: If a file with the same name as a target exists, make will think that the target is up to date and will not execute associated commands. .PHONY avoids this.

  • Performance Improvements: Phony targets are always considered "to do", which may improve the speed of execution of associated commands.

Why use %.o:%c for compilation?

  • Efficiency: Using %.o:%c allows you to benefit from make optimization to only recompile what is necessary.

  • Practical: For larger projects, %.o:%c is much more suitable.

Conclusion

A well-structured Makefile makes project management easier and avoids compilation errors. By following these rules and best practices, you can create an efficient and maintainable Makefile.

Release Statement This article is reproduced at: https://dev.to/ashcript/comprendre-le-makefile-exemple-avec-le-langage-c-47n9 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3