Heim  >  Artikel  >  Backend-Entwicklung  >  Makefile – .h – .c-Beispiel.

Makefile – .h – .c-Beispiel.

王林
王林Original
2024-07-17 07:42:59416Durchsuche

Makefile - .h - .c exemple.

Hier ist die Projektstruktur mit dem Beispiel ohne statische Bibliothek zuerst, gefolgt vom Beispiel mit statischer Bibliothek.

Projektstruktur

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

Beispiel 1: Ohne statische Bibliothek

1. Header-Datei: utils.h

#ifndef UTILS_H
#define UTILS_H

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

#endif // UTILS_H

2. Quelldatei: utils.c

#include "utils.h"

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

3. Hauptdatei: 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

Beispiel 2: Mit einer statischen Bibliothek

1. Header-Datei: utils.h

#ifndef UTILS_H
#define UTILS_H

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

#endif // UTILS_H

2. Quelldatei: utils.c

#include "utils.h"

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

3. Hauptdatei: 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

Zusammenfassung der Beispiele

  1. Ohne statische Bibliothek:

    • Kompiliert die Quelldateien direkt, um die ausführbare Datei „my_program“ zu erstellen, ohne eine Bibliothek zu erstellen.
  2. Mit statischer Bibliothek:

    • Erstellt eine libutils.a-Bibliothek aus utils.o.
    • Die ausführbare Datei „my_program“ hängt von dieser Bibliothek ab.

Verwenden

  • Um das Programm zu kompilieren: make
  • Um Objektdateien und die Bibliothek zu bereinigen (im ersten Beispiel): make clean
  • Um vollständig zu reinigen: machen Sie fclean
  • Zum Wiederaufbau: Machen Sie neu

Diese Beispiele zeigen, wie man ein einfaches Projekt mit und ohne statischer Bibliothek strukturiert und dabei die Klarheit und Wartbarkeit im Makefile beibehält.

Beispiel 3: Verwendung einer anderen Bibliothek:

Hinweis: Dies ist das Makefile, das ich bei der Durchführung eines meiner Projekte erstellt habe.

# 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

Wichtige Verbesserungen

  1. Automatische Generierung von Objektdateien: Die Variable OBJ_FILES konvertiert mithilfe von Musterersetzung automatisch Quelldateinamen in Objektdateinamen.

  2. Musterregeln: Die Verwendung von Musterregeln (%.o:%c) vereinfacht Kompilierungsbefehle für jede Quelldatei.

  3. Regeln für die organisierte Reinigung: Die Reinigungsregeln sind prägnant und vermeiden unnötige Wiederholungen.

  4. Einfache Wartung: Die Struktur ist klar, was zukünftige Änderungen einfacher macht.

Dieses Makefile behält die gleiche Funktionalität bei und ist gleichzeitig sauberer und effizienter.

Das obige ist der detaillierte Inhalt vonMakefile – .h – .c-Beispiel.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn