如何让 Makefile 自动重建包含修改后的头文件的源文件?(在 C/C++ 中) [英] How can I have a Makefile automatically rebuild source files that include a modified header file? (In C/C++)

查看:27
本文介绍了如何让 Makefile 自动重建包含修改后的头文件的源文件?(在 C/C++ 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下生成文件,用于构建我正在处理的程序(实际上是内核).它是从头开始的,我正在学习这个过程,所以它并不完美,但我认为它在这一点上足够强大,足以满足我编写 makefile 的经验水平.

I have the following makefile that I use to build a program (a kernel, actually) that I'm working on. Its from scratch and I'm learning about the process, so its not perfect, but I think its powerful enough at this point for my level of experience writing makefiles.

AS  =   nasm
CC  =   gcc
LD  =   ld

TARGET      =   core
BUILD       =   build
SOURCES     =   source
INCLUDE     =   include
ASM         =   assembly

VPATH = $(SOURCES)

CFLAGS  =   -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions 
            -nostdinc -fno-builtin -I $(INCLUDE)
ASFLAGS =   -f elf

#CFILES     =   core.c consoleio.c system.c
CFILES      =   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
SFILES      =   assembly/start.asm

SOBJS   =   $(SFILES:.asm=.o)
COBJS   =   $(CFILES:.c=.o)
OBJS    =   $(SOBJS) $(COBJS)

build : $(TARGET).img

$(TARGET).img : $(TARGET).elf
    c:/python26/python.exe concat.py stage1 stage2 pad.bin core.elf floppy.img

$(TARGET).elf : $(OBJS)
    $(LD) -T link.ld -o $@ $^

$(SOBJS) : $(SFILES)
    $(AS) $(ASFLAGS) $< -o $@

%.o: %.c
    @echo Compiling $<...
    $(CC) $(CFLAGS) -c -o $@ $<

#Clean Script - Should clear out all .o files everywhere and all that.
clean:
    -del *.img
    -del *.o
    -del assembly*.o
    -del core.elf

我对这个 makefile 的主要问题是,当我修改一个或多个 C 文件包含的头文件时,不会重建 C 文件.通过让我的所有头文件成为我所有 C 文件的依赖项,我可以很容易地解决这个问题,但这会在我更改/添加头文件时有效地导致项目的完全重建,这不会很优雅.

My main issue with this makefile is that when I modify a header file that one or more C files include, the C files aren't rebuilt. I can fix this quite easily by having all of my header files be dependencies for all of my C files, but that would effectively cause a complete rebuild of the project any time I changed/added a header file, which would not be very graceful.

我想要的是仅重新构建包含我更改的头文件的 C 文件,并重新链接整个项目.我可以通过使所有头文件成为目标的依赖项来进行链接,但是当包含的头文件更新时,我无法弄清楚如何使 C 文件失效.

What I want is for only the C files that include the header file I change to be rebuilt, and for the entire project to be linked again. I can do the linking by causing all header files to be dependencies of the target, but I cannot figure out how to make the C files be invalidated when their included header files are newer.

我听说 GCC 有一些命令可以使这成为可能(因此 makefile 可以以某种方式确定需要重建哪些文件),但我一生都找不到要查看的实际实现示例.有人可以发布一个解决方案来在 makefile 中启用此行为吗?

I've heard that GCC has some commands to make this possible (so the makefile can somehow figure out which files need to be rebuilt) but I can't for the life of me find an actual implementation example to look at. Can someone post a solution that will enable this behavior in a makefile?

我应该澄清一下,我熟悉将单个目标放入并让每个 target.o 需要头文件的概念.这要求我每次在某处包含头文件时都要编辑 makefile,这有点麻烦.我正在寻找一种可以自行派生头文件依赖项的解决方案,我很确定我已经在其他项目中看到过.

I should clarify, I'm familiar with the concept of putting the individual targets in and having each target.o require the header files. That requires me to be editing the makefile every time I include a header file somewhere, which is a bit of a pain. I'm looking for a solution that can derive the header file dependencies on its own, which I'm fairly certain I've seen in other projects.

推荐答案

正如本网站其他地方已经指出的那样,请参阅此页面:自动依赖生成

As already pointed out elsewhere on this site, see this page: Auto-Dependency Generation

简而言之,gcc 可以自动为您创建 .d 依赖文件,它们是包含您编译的 .c 文件的依赖项的迷你 makefile 片段.每次更改 .c 文件并编译时,都会更新 .d 文件.

In short, gcc can automatically create .d dependency files for you, which are mini makefile fragments containing the dependencies of the .c file you compiled. Every time you change the .c file and compile it, the .d file will be updated.

除了将 -M 标志添加到 gcc 之外,您还需要在 makefile 中包含 .d 文件(就像 Chris 上面写的那样).页面中有一些更复杂的问题可以使用 sed 解决,但是您可以忽略它们并执行make clean"以清除 .d 文件,每当 make 抱怨无法构建不再存在的头文件时.

Besides adding the -M flag to gcc, you'll need to include the .d files in the makefile (like Chris wrote above). There are some more complicated issues in the page which are solved using sed, but you can ignore them and do a "make clean" to clear away the .d files whenever make complains about not being able to build a header file that no longer exists.

这篇关于如何让 Makefile 自动重建包含修改后的头文件的源文件?(在 C/C++ 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆