Home  >  Article  >  Backend Development  >  Makefile - .h - .c exemple.

Makefile - .h - .c exemple.

王林
王林Original
2024-07-17 07:42:59415browse

Makefile - .h - .c exemple.

Here is the project structure with the example without static library first, followed by the example with static library.

Project Structure

/mon_projet
├── Makefile
├── utils.h
├── utils.c
└── main.c

Example 1: Without static library

1. Header file: utils.h

#ifndef UTILS_H
#define UTILS_H

// Fonction pour additionner deux entiers
int addition(int a, int b);

#endif // UTILS_H

2. Source file: utils.c

#include "utils.h"

// Implémentation de la fonction d'addition
int addition(int a, int b) {
    return a + b;
}

3. Main file: main.c

#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;
}

4. Makefile: Makefile

# 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

Example 2: With a static library

1. Header file: utils.h

#ifndef UTILS_H
#define UTILS_H

// Fonction pour additionner deux entiers
int addition(int a, int b);

#endif // UTILS_H

2. Source file: utils.c

#include "utils.h"

// Implémentation de la fonction d'addition
int addition(int a, int b) {
    return a + b;
}

3. Main file: main.c

#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;
}

4. Makefile: Makefile

# 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

Summary of Examples

  1. Without static library:

    • Directly compiles the source files to create the my_program executable without creating a library.
  2. With static library:

    • Creates a libutils.a library from utils.o.
    • The my_program executable depends on this library.

Use

  • To compile the program: make
  • To clean object files and the library (in the first example): make clean
  • To clean completely: make fclean
  • To rebuild: make re

These examples show how to structure a simple project with and without a static library while maintaining clarity and maintainability in the Makefile.

Example 3: Using another library:

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

Key Improvements

  1. Automatic Generation of Object Files: The OBJ_FILES variable automatically converts source file names to object file names using pattern substitution.

  2. Pattern Rules: Using pattern rules (%.o:%c) simplifies compilation commands for each source file.

  3. Organized Cleaning Rules: The cleaning rules are concise, removing unnecessary repetition.

  4. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn