简体   繁体   English

修改CFLAGS时,Makefile不会重建obj?

[英]Makefile doesn't rebuild the obj's when the CFLAGS are modified?

As we know that the binary depends on the obj's, and the obj's depends on the .c files ( assuming a C Project). 我们知道二进制文件依赖于obj,而obj依赖于.c文件(假设是C项目)。 Let's say, I have a env.mk file. 比方说,我有一个env.mk文件。 This file has a flag like 'export NO_DISPLAY=YES'. 此文件有一个标记,如'export NO_DISPLAY = YES'。 In the main Makefile, I have the following. 在主Makefile中,我有以下内容。

ifeq ($(NO_DISPLAY),YES)
CFLAGS += -D__DISPLAY_DISABLE
endif

Obviously, env.mk is included in the main make file. 显然,env.mk包含在主make文件中。 whenever, I change the flag value 'NO_DISPLAY'. 每当,我改变标志值'NO_DISPLAY'。 The makefile never rebuilts the executable again. makefile永远不会再次重建可执行文件。 However, the same works fine when the .o files are deleted. 但是,删除.o文件时,同样可以正常工作。 I understand that the reason behind it as it depends on the .c,.h files. 我理解它背后的原因,因为它取决于.c,.h文件。 The .c .h files are not modified, therefore makefile ignores to rebuild it. .c .h文件未被修改,因此makefile忽略重建它。 But, I would like makefile to rebuild the code if the CFLAGS value is changed. 但是,如果更改CFLAGS值,我希望makefile重建代码。 How can I do it? 我该怎么做? Please note, I don't want to delete the objs and rebuild it. 请注意,我不想删除obj并重建它。

target_dbg: $(patsubst ./src/%.c,./obj_dbg/%.o,$(wildcard ./src/*.c)) 
    @echo "Target main rule__dbg $(NPROCS)"
    $(CC) $(patsubst ./src/%.c,./obj_dbg/%.o,$(wildcard ./src/*.c)) $(LIBS) -o gif_dbg 

./obj_dbg/%.o: ./src/%.c ./include/*.h 
    @echo "I am called first..dbg"
    @mkdir -p ./obj_dbg
    #$(CC) $(CFLAGS) -E $<
    $(CC) $(CFLAGS) $(LDFLAGS) -DDEBUG -c $< -o $@

Any help will be appreciated. 任何帮助将不胜感激。

Make simply works by examining timestamps on files. 通过检查文件的时间戳来简化工作。 You hardly want every build artefact to depend on your Makefile (at least not while actively developing it) but if you seriously want Make to handle this dependency, you could put the CFLAGS definition in a secondary file buildflags.mk , include it from the main Makefile , and make all object files depend on buildflags.mk . 你几乎不希望每个构建工件依赖于你的Makefile (至少在积极开发它时没有)但是如果你真的想要Make来处理这种依赖,你可以将CFLAGS定义放在辅助文件buildflags.mk ,从主文件中包含它Makefile ,并使所有目标文件都依赖于buildflags.mk

I hardly think anybody would actually do this in practice, though. 不过,我几乎认为没有人会在实践中真正做到这一点。 There will always be situations where the only way to be sure you get a clean build is to flush everything and start over. 总会有这样的情况,确保你获得一个干净的构建的唯一方法是刷新一切并重新开始。 Make sure you have good and up-to-date realclean and/or distclean targets, and make sure you remember to use them when you make fundamental changes to your build infrastructure. 确保您拥有良好且最新的realclean和/或distclean目标,并确保在对构建基础结构进行基本更改时记住使用它们。 Having a nightly build job (or similar) which starts the build from a completely clean slate -- eg by checking out a new copy into a temporary directory -- is also obviously a good idea. 有一个夜间构建工作(或类似的)从一个完全干净的平板开始构建 - 例如通过检查一个新的副本到一个临时目录 - 显然也是一个好主意。

Alternatively, or additionally, include a copy of the build flags as a static string in each object file, so you can verify them later, perhaps using a --help option or similar. 或者,或者另外,在每个目标文件中包含构建标志的副本作为静态字符串,因此您可以稍后验证它们,可能使用--help选项或类似方法。

You could use make 's -B option to force a rebuild each time you change your CFLAGS . 每次更改CFLAGS时,都可以使用make-B选项强制重建。 See this answer . 看到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM