如何获得目标的最后依赖?

ane*_*eal 1 makefile

让我说我有:

target.o: target.h target.c
    gcc $(CFLAGS) -c target.c
Run Code Online (Sandbox Code Playgroud)

但我想摆脱冗余的'target.c'.我知道$ <将给target.h,是否有一个内部宏将给target.c或者我应该重新排列它:

target.o: target.c target.h
    gcc $(CFLAGS) -c $<
Run Code Online (Sandbox Code Playgroud)

但是......我似乎记得在这种情况下$ <并不会总是返回target.c,例如,如果target.h中的更改触发了这个规则,那么$ <将返回target.h.那么有没有办法一直这样做?

Oli*_*rth 6

你应该使用你的第二个例子:

target.o: target.c target.h
    gcc $(CFLAGS) -c $<
Run Code Online (Sandbox Code Playgroud)

$<总是评估第一个先决条件; 哪一个导致配方运行无关紧要.有关详细信息,请参阅http://www.gnu.org/software/make/manual/make.html#Automatic-Variables.

如果你真的是偏执狂,你可以随时做到这一点(但这是不必要的):

target.o: target.c target.h
    gcc $(CFLAGS) -c $(filter %.c,$^)
Run Code Online (Sandbox Code Playgroud)