make: .SECONDARY target breaks 灯泡

make: .SECONDARY target breaks buld

我的目标是生成一个文件(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:,依赖链似乎 "broken",因为它在第一个目标(MyOut.elf):

> del *.elf                                

> make                                     
Building ELF: MyOut.elf                    
Finished building target: MyOut.elf        

TARGET                                     

然后,如果我再次运行 make,就会构建依赖文件MyOut.s19size.txt

> make                                     
Building S19 : MyOut.s19                   
Finished building target: MyOut.s19        

Building section size summary : size.txt   
Finished building target: size.txt         

TARGET                                     

所以,我的问题是:

我找到了一个解决方案:我在目标 full:

的先决条件中添加了一个新的 "dummy" 目标 otherTargets
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 文件,"dependant" 文件会正确重建。

我找到的解释与 GNU make documentation:

有关

If an ordinary file b does not exist, and make considers a target that depends on b, it invariably creates b and then updates the target from b. But if b is an intermediate file, then make can leave well enough alone. It won’t bother updating b, or the ultimate target, unless some prerequisite of b is newer than that target or there is some other reason to update that target.

因此,我的"intermediate files"MyOut.s19size.txt没有重建。 然后我的解决方案开始工作,因为目标 otherTargets 永远不会生成输出文件,因此它总是需要更新。