Home > Article > Backend Development > Makefile - .h - .c exemple.
Here is the project structure with the example without static library first, followed by the example with static library.
/mon_projet ├── Makefile ├── utils.h ├── utils.c └── main.c
#ifndef UTILS_H #define UTILS_H // Fonction pour additionner deux entiers int addition(int a, int b); #endif // UTILS_H
#include "utils.h" // Implémentation de la fonction d'addition int addition(int a, int b) { return a + b; }
#include <stdio.h> #include "utils.h" int main() { int a = 5; int b = 3; int result = addition(a, b); printf("La somme de %d et %d est : %d\n", a, b, result); return 0; }
# Variables CC = gcc CFLAGS = -Wall -g SOURCES = main.c utils.c OBJECTS = $(SOURCES:.c=.o) DEPENDS = $(OBJECTS:.o=.d) TARGET = mon_programme # Règle par défaut all: $(TARGET) # Lien de l'exécutable $(TARGET): $(OBJECTS) $(CC) -o $@ $^ # Compilation des fichiers .c en .o avec génération des dépendances %.o: %.c $(CC) $(CFLAGS) -MMD -c $< -o $@ # Inclure les fichiers de dépendance -include $(DEPENDS) # Déclaration des cibles phony .PHONY: all clean fclean re # Nettoyage clean: rm -f $(OBJECTS) $(DEPENDS) fclean: clean rm -f $(TARGET) re: fclean all
#ifndef UTILS_H #define UTILS_H // Fonction pour additionner deux entiers int addition(int a, int b); #endif // UTILS_H
#include "utils.h" // Implémentation de la fonction d'addition int addition(int a, int b) { return a + b; }
#include <stdio.h> #include "utils.h" int main() { int a = 5; int b = 3; int result = addition(a, b); printf("La somme de %d et %d est : %d\n", a, b, result); return 0; }
# Variables CC = gcc AR = ar CFLAGS = -Wall -g SOURCES = main.c utils.c OBJECTS = $(SOURCES:.c=.o) DEPENDS = $(OBJECTS:.o=.d) TARGET = mon_programme LIBRARY = libutils.a # Règle par défaut all: $(TARGET) # Lien de l'exécutable $(TARGET): $(OBJECTS) $(LIBRARY) $(CC) -o $@ $^ # Création de la bibliothèque statique $(LIBRARY): utils.o $(AR) rcs $@ $^ # Compilation des fichiers .c en .o avec génération des dépendances %.o: %.c $(CC) $(CFLAGS) -MMD -c $< -o $@ # Inclure les fichiers de dépendance -include $(DEPENDS) # Déclaration des cibles phony .PHONY: all clean fclean re # Nettoyage clean: rm -f $(OBJECTS) $(DEPENDS) $(LIBRARY) fclean: clean rm -f $(TARGET) re: fclean all
Without static library:
With static library:
These examples show how to structure a simple project with and without a static library while maintaining clarity and maintainability in the Makefile.
Note: this is the Makefile that I created when carrying out one of my projects.
# Arguments NAME = libftprintf.a CFLAGS = -Wall -Wextra -Werror -I . # Sources SRC_FILES = ft_printf.c \ ft_ulitob.c \ ft_putunbr_fd.c \ ft_unsigned_lintlen.c \ ft_lintlen.c \ ft_print_c.c \ ft_print_s.c \ ft_print_p.c \ ft_print_di.c \ ft_print_u.c \ ft_print_x.c # Objets OBJ_FILES = $(SRC_FILES:.c=.o) # Règle principale all: $(NAME) # Création de la bibliothèque $(NAME): $(OBJ_FILES) make -C libft/ cp libft/libft.a $(NAME) ar rcs $(NAME) $(OBJ_FILES) # Compilation des fichiers source %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ # Nettoyage clean: rm -rf $(OBJ_FILES) make clean -C libft/ fclean: clean rm -rf $(NAME) make fclean -C libft/ re: fclean all # Commandes indispensables .PHONY: all clean fclean re
Automatic Generation of Object Files: The OBJ_FILES variable automatically converts source file names to object file names using pattern substitution.
Pattern Rules: Using pattern rules (%.o:%c) simplifies compilation commands for each source file.
Organized Cleaning Rules: The cleaning rules are concise, removing unnecessary repetition.
Ease of Maintenance: The structure is clear, making future modifications simpler.
This Makefile maintains the same functionality while being cleaner and more efficient.
The above is the detailed content of Makefile - .h - .c exemple.. For more information, please follow other related articles on the PHP Chinese website!