gpt4 book ai didi

makefile - 如何获取目标的最后一个依赖项?

转载 作者:行者123 更新时间:2023-12-02 21:35:22 26 4
gpt4 key购买 nike

假设我有:

target.o: target.h target.c
gcc $(CFLAGS) -c target.c

但我想去掉多余的“target.c”。我知道 $< 将给出 target.h,是否有一个内部宏将给出 target.c 或者我应该重新排列它:

target.o: target.c target.h
gcc $(CFLAGS) -c $<

但是...我似乎记得在这种情况下 $< 并不总是返回 target.c,例如,如果 target.h 中的更改触发了此规则,则 $< 将返回 target.h。那么有什么方法可以始终如一地做到这一点吗?

最佳答案

您应该只使用第二个示例:

target.o: target.c target.h
gcc $(CFLAGS) -c $<

$<始终评估第一个先决条件;哪一个导致配方运行并不重要。欲了解更多信息,请参阅http://www.gnu.org/software/make/manual/make.html#Automatic-Variables .

如果你真的很偏执,你总是可以这样做(但这是不必要的):

target.o: target.c target.h
gcc $(CFLAGS) -c $(filter %.c,$^)

关于makefile - 如何获取目标的最后一个依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9847535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com