首頁 > 後端開發 > C++ > 主體

Makefile - .h - .c 範例。

王林
發布: 2024-07-17 07:42:59
原創
333 人瀏覽過

Makefile - .h - .c exemple.

這是專案結構,首先是沒有靜態函式庫的範例,然後是帶有靜態函式庫的範例。

專案結構

/mon_projet
├── Makefile
├── utils.h
├── utils.c
└── main.c
登入後複製

範例1:沒有靜態庫

1.頭檔:utils.h

#ifndef UTILS_H
#define UTILS_H

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

#endif // UTILS_H
登入後複製
登入後複製

2.原始檔:utils.c

#include "utils.h"

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

3.主檔案: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

# 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
登入後複製

範例 2:使用靜態函式庫

1.頭檔:utils.h

#ifndef UTILS_H
#define UTILS_H

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

#endif // UTILS_H
登入後複製
登入後複製

2.原始檔:utils.c

#include "utils.h"

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

3.主檔案: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

# 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
登入後複製

例子總結

  1. 沒有靜態庫:

    • 直接編譯原始檔以建立 my_program 執行文件,無需建立庫。
  2. 使用靜態庫:

    • 從 utils.o 建立 libutils.a 函式庫。
    • my_program 執行檔依賴於該程式庫。

使用

  • 編譯程式:make
  • 清理目標檔案和函式庫(在第一個範例中):make clean
  • 徹底清理:make fclean
  • 重建:make re

這些範例顯示如何在使用和不使用靜態函式庫的情況下建構一個簡單的項目,同時保持 Makefile 的清晰度和可維護性。

範例 3:使用另一個庫:

注意:這是我執行一個專案時所建立的 Makefile。

# 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
登入後複製

主要改進

  1. 自動產生目標檔案:OBJ_FILES 變數使用模式取代會自動將來源檔案名稱轉換為目標檔案名稱。

  2. 模式規則:使用模式規則(%.o:%c)簡化每個原始檔的編譯指令。

  3. 有組織的清潔規則:清潔規則簡潔,消除不必要的重複。

  4. 易於維護:結構清晰,方便日後修改。

這個 Makefile 保持了相同的功能,同時更乾淨、更有效率。

以上是Makefile - .h - .c 範例。的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!