将Makefile中的长依赖项分成几行

时间:2016-06-28 21:23:03

标签: makefile

target: TargetA ../DirB/FileB.cpp ../DirC/FileC.o ../DirD/FileD.o ...

这是一个make文件中的长行。有可能把它分成几行吗?

2 个答案:

答案 0 :(得分:8)

来自文档https://www.gnu.org/software/make/manual/make.html#Splitting-Lines

  

Makefile使用“基于行”的语法,其中换行符是特殊的,并标记语句的结尾。 GNU make对语句行的长度没有限制,最多可以计算机中的内存量。

     

然而,如果没有包装或滚动,很难读取太长而无法显示的行。因此,您可以通过在语句中间添加换行符来格式化makefile以提高可读性:您可以通过使用反斜杠(\)字符转义内部换行来完成此操作。

它也适用于目标,即:

a b c d:
        touch $@

multiline_dependencies: a \
 b \
 c \
 d
        touch $@

并验证,make multiline_dependencies --dry-run提供以下输出

touch a
touch b
touch c
touch d
touch multiline_dependencies

答案 1 :(得分:7)

有几种方法可以做到这一点。一个简单的方法:

target: targetA targetB
target: targetC targetD

target:
    @echo $@ is dependent on $?

请注意,这不适用于模式规则(目标/依赖项中包含%的规则)。如果您正在使用模式规则(即使您不是),您可以考虑采取以下措施:

TARGET_DEPS := targetA targetB
TARGET_DEPS += targetC
TARGET_DEPS += targetD

target: $(TARGET_DEPS)
   @echo $@ is dependent on $?

虽然可以使用反斜杠,但我个人认为这使得makefile更难以阅读,因为缩进的含义变得不清楚。

相关问题