头文件更改期间要重建的Makefile [英] Makefile to rebuild during header file changes
问题描述
我正在尝试为项目编写Makefile.这样一来,只要任何C文件发生更改,我都可以构建项目.但是,当头文件发生更改时,它不会生成.
I'm trying to write a Makefile for a project. With that, I was able to build the project whenever there is a change in any of the C files. But, it doesn't build when there is a change in a header file.
因此,我正在尝试使用Makefile创建更简单的目录结构,如果可以,我计划在主项目中执行相同的操作.
So, I'm trying Makefile for a simpler directory structure and if it works I'm planning to do the same in the main project.
目录结构为
src -> code1 -> file1.c
src -> code2 -> file2.c
src -> code1inc -> file1.h
src -> code2inc -> file2.h
所有文件都应一起构建并给出一个二进制 FinalBin
All the files together should build and give a single binary FinalBin
当file1.h中有更改时,它仅需构建file1.c.我该如何通过Makefile强加这个?
When there is a change in file1.h, it has to build only file1.c. How do I impose this through the Makefile?
这是我当前的Makefile.
This is my current Makefile.
CC = gcc
CFLAGS = $(INCLUDES)
TARGET = FinalBin
SOURCES := $(wildcard *.c)
PATH_CODE1 = src/code1
PATH_CODE2 = src/code2
CODE1_SOURCES := $(wildcard $(PATH_CODE1)/*.c)
CODE1_OBJECTS := $(patsubst %.c,%.o,$(CODE1_SOURCES))
CODE2_SOURCES := $(wildcard $(PATH_CODE2)/*.c)
CODE2_OBJECTS := $(patsubst %.c,%.o,$(CODE2_SOURCES))
PATH_CODE1INC = src/code1inc
PATH_CODE2INC = src/code2inc
INCLUDES = -I$(PATH_CODE1INC) \
-I$(PATH_CODE2INC)
CODE1_HEADERS := $(wildcard $(PATH_CODE1INC)/*.h)
CODE2_HEADERS := $(wildcard $(PATH_CODE2INC)/*.h)
all: $(TARGET)
obj1 = $(CODE1_OBJECTS) $(CODE1_HEADERS)
obj2 = $(CODE2_OBJECTS) $(CODE2_HEADERS)
$(TARGET) : $(obj1) $(obj2)
$(CC) $^ -o $@
clean:
rm $(TARGET) $(CODE1_OBJECTS) $(CODE2_OBJECTS)
请指导我.
推荐答案
不需要规则中的sed语句.Gcc支持-MF选项来控制依赖项输出文件.
The sed statements in the rule are not needed. Gcc supports the -MF option to control the dependency output file.
CC = gcc
SRCS = foo.c bar.c ...
OBJS = $(SRCS:%.c=%.o)
DEPS = $(wildcard $(OBJS:%=%.d))
%.o : %.c
$(CC) -MD -MP -MF "$@.d" -o $@ -c $<
include $(DEPS)
这篇关于头文件更改期间要重建的Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!