我的目标是生成一个文件(MyOut.elf),如果生成此文件,则生成依赖于该文件的其他两个文件(MyOut.s19和size.txt) 。
我是关于makefile的新手,但我写了以下内容:
.DEFAULT_GOAL := full
full: MyOut.elf MyOut.s19 size.txt
@echo TARGET
# Tool invocations
MyOut.s19: MyOut.elf
@echo 'Building S19 : $@'
@echo 'MyOut.s19: MyOut.elf' > MyOut.s19
@echo 'Finished building target: $@'
@echo ' '
size.txt: MyOut.elf
@echo 'Building section size summary : $@'
@echo 'size.txt: MyOut.elf' > size.txt
@echo 'Finished building target: $@'
@echo ' '
all: MyOut.elf
MyOut.elf:
@echo 'Building ELF: $@'
@echo 'MyOut.elf' > MyOut.elf
@echo 'Finished building target: $@'
@echo ' '
.PHONY: full
#.SECONDARY:
现在,如果我运行make
,一切都按预期工作:
> make
Building ELF: MyOut.elf
Finished building target: MyOut.elf
Building S19 : MyOut.s19
Finished building target: MyOut.s19
Building section size summary : size.txt
Finished building target: size.txt
TARGET
相反,如果我取消注释最后一行.SECONDARY:
,依赖关系链似乎"已损坏",因为它在第一个目标(MyOut.elf)之后停止:
> del *.elf
> make
Building ELF: MyOut.elf
Finished building target: MyOut.elf
TARGET
然后,如果我再次运行make,则构建依赖文件MyOut.s19
和size.txt
:
> make
Building S19 : MyOut.s19
Finished building target: MyOut.s19
Building section size summary : size.txt
Finished building target: size.txt
TARGET
所以,我的问题是:
答案 0 :(得分:0)
我找到了解决方案:我添加了一个新的"虚拟"将otherTargets
定位到目标full
的先决条件:
full: MyOut.elf otherTargets
otherTargets: MyOut.s19 size.txt
现在,当我运行make
时,我得到了
Building ELF: MyOut.elf
Finished building target: MyOut.elf
Building section size summary : size.txt
Finished building target: size.txt
Building S19 : MyOut.s19
Finished building target: MyOut.s19
如果我第二次运行它,我会
make: Nothing to be done for `full'.
尽管如此,如果我删除了.elf
文件,那么"依赖"文件正确重建。
我发现的解释与GNU make documentation:
有关如果普通文件b不存在,并且make考虑依赖于b的目标,则它总是创建b,然后从b更新目标。但是如果b是一个中间文件,那么make就可以单独留下。它不会打扰更新b或最终目标,除非b的某些先决条件比该目标更新或者有其他理由更新该目标。
因此,我的"中间文件" MyOut.s19
和size.txt
未重建。
我的解决方案正在运行,因为目标otherTargets
永远不会生成输出文件,因此它总是需要更新。