Makefile 使始终重建 问题
我正试图重构我的Makefile,以反映中给出的见解。我的用例和本文描述的用例之间的区别在于,它认为可以接受将对象和依赖项文件与源文件和头文件放在同一目录中,这对于我的用例来说是不可取的 依赖项文件生成Makefile 使始终重建 问题,makefile,Makefile,我正试图重构我的Makefile,以反映中给出的见解。我的用例和本文描述的用例之间的区别在于,它认为可以接受将对象和依赖项文件与源文件和头文件放在同一目录中,这对于我的用例来说是不可取的 依赖项文件生成 depend.sh脚本是从纸上复制的。它根据子目录的布局使用gcc中的数据生成一个依赖项文件。除非我打错了,否则我认为错误不在这里: #!/bin/sh DIR="$1" shift 1 case "$DIR" in "" | ".") g++ -std=c++0x -MM -MG "$@"
depend.sh
脚本是从纸上复制的。它根据子目录的布局使用gcc中的数据生成一个依赖项文件。除非我打错了,否则我认为错误不在这里:
#!/bin/sh
DIR="$1"
shift 1
case "$DIR" in
"" | ".")
g++ -std=c++0x -MM -MG "$@" | sed -e 's@^\(.*\)\.o:@\1.d \1.o:@'
;;
*)
g++ -std=c++0x -MM -MG "$@" | sed -e "s@^\(.*\)\.o:@$DIR/\1.d $DIR/\1.o:@"
;;
esac
我的代码树类似于下面的代码树:
问题:
我做了一些修改,生成的文件如下所示。当我运行makenew\u make
时,即使依赖项文件是正确的,它也总是重新编译,这是我做错了什么
另外,如果依赖项文件对头文件有要求,那么对它的任何更改都会导致它的重新编译,这是正确的吗
Makefile示例
src/MavCommunication.d的依赖关系文件示例
src/MavCommunication.d部分的make-d输出
在这里,我认为make不尊重.d的.h要求
Considering target file `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Looking for an implicit rule for `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Trying pattern rule with stem `src/MavCommunication'.
Trying implicit prerequisite `src/MavCommunication.cpp'.
Found an implicit rule for `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Considering target file `src/MavCommunication.cpp'.
Looking for an implicit rule for `src/MavCommunication.cpp'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/MavCommunication.cpp,v'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/RCS/MavCommunication.cpp,v'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/RCS/MavCommunication.cpp'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/s.MavCommunication.cpp'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/SCCS/s.MavCommunication.cpp'.
No implicit rule found for `src/MavCommunication.cpp'.
Finished prerequisites of target file `src/MavCommunication.cpp'.
No need to remake target `src/MavCommunication.cpp'.
Finished prerequisites of target file `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Prerequisite `src/MavCommunication.cpp' is older than target `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
check\u dirs
是一个虚假的目标
.PHONY
目标不能(也不应该)成为真正目标的先决条件,正如手册中的一节所述:
虚假目标不应是真实目标文件的先决条件;如果是,它的配方将在make每次更新该文件时运行。只要虚假目标永远不是真实目标的先决条件,那么只有当虚假目标是指定目标时才会执行虚假目标配方(请参见指定目标的参数)
此外,运行make-d
的输出将告诉您make在做什么以及为什么要这样做
此外,该makefile中用于.d
文件的路径不一致
您告诉make从.o
文件旁边加载文件(-include$(OBJ:.o=.d)
),但您告诉make在.c
文件$(OBJECTS\u DIRECTORY)/%旁边创建.d
文件。这几乎肯定会导致make无法找到要包含的.d
文件。为什么不迁移到SCONS或Gradle之类的生成系统?check_dirs
是一个虚假的目标。它们不能成为真正目标的先决条件。请参阅GNU Make手册中的引用。同时运行make-d
,看看它说它在做什么以及为什么。另外,您似乎在告诉make从对象文件旁边加载.d
文件,但告诉make在.cpp
文件旁边创建.d
文件,不是吗$(OBJECTS\u DIRECTORY)/%.o:%.d
,%.d:%.cpp
和-包括$(OBJ:.o=.d)
@Etan Reisner:你能把它写出来作为答案吗?@Etan Reisner:.phoney目标的问题不是主要问题,尽管我已经纠正了。另一方面,错误生成的.d文件的问题是半正确的。在更正它们之后,不再进行重建,但是如果我更改.h文件,就不会像应该的那样进行重新编译。
src/MavCommunication.d src/MavCommunication.o: src/MavCommunication.cpp src/MavCommunication.h \
src/SerialCommunication.h src/ICommunication.h src/Hardware.h \
src/IColor.h mavlink/common/mavlink.h src/ActionRequestState.h \
src/Waypoint.h mavlink/ardupilotmega/mavlink.h \
mavlink/ardupilotmega/ardupilotmega.h
Considering target file `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Looking for an implicit rule for `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Trying pattern rule with stem `src/MavCommunication'.
Trying implicit prerequisite `src/MavCommunication.cpp'.
Found an implicit rule for `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Considering target file `src/MavCommunication.cpp'.
Looking for an implicit rule for `src/MavCommunication.cpp'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/MavCommunication.cpp,v'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/RCS/MavCommunication.cpp,v'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/RCS/MavCommunication.cpp'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/s.MavCommunication.cpp'.
Trying pattern rule with stem `MavCommunication.cpp'.
Trying implicit prerequisite `src/SCCS/s.MavCommunication.cpp'.
No implicit rule found for `src/MavCommunication.cpp'.
Finished prerequisites of target file `src/MavCommunication.cpp'.
No need to remake target `src/MavCommunication.cpp'.
Finished prerequisites of target file `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.
Prerequisite `src/MavCommunication.cpp' is older than target `/media/truecrypt1/Projects/master_thesis.master/Software/obj/src/MavCommunication.d'.