首页  >  文章  >  后端开发  >  Makefile - .h - .c 示例。

Makefile - .h - .c 示例。

王林
王林原创
2024-07-17 07:42:59416浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn