GNU make:单个规则中有多个目标 [英] GNU make: Multiple targets in a single rule

查看:93
本文介绍了GNU make:单个规则中有多个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
GNU Makefile规则从中生成一些目标单个源文件

Possible Duplicate:
GNU Makefile rule generating a few targets from a single source file

如果我有这样的Makefile规则:

If I have a Makefile rule like this:

a b c:
    echo "Creating a b c"
    touch a b c

output: a b c
    cat a b c > output

然后运行 make -j9输出

make查看3个依赖项(a,b,c),寻找产生它们的方式:(上面的"a b c"规则),但是接下来会发生什么呢?是否不应该意识到只需要运行一次"a b c"规则即可创建所有3个目标?

make sees the 3 dependencies (a,b,c), looks for how to produce them: (the "a b c" rule above), but what happens next? Should it not realize that the "a b c" rule only needs to be run once to create all 3 targets?

这是make实际执行的操作:

This is what make actually does:

[pavel@orianna test]$ make -j9 output -n
echo "Creating a b c"
touch a b c
echo "Creating a b c"
touch a b c
echo "Creating a b c"
touch a b c                                                                                                                                                                                                                                                                    
cat a b c > output                                                                                                                                                                                                                                                             
[pavel@orianna test]$

同一配方运行 3次,对于每个依赖项一次以规则输出"!

The same recipe is run 3 times, once for each dependency to rule "output"!

有人知道为什么会这样吗?

Does anyone know why it behaves this way?

推荐答案

您的a b c:规则告诉Make,这是如何构建这些目标的 any ,而不是如何构建 all . Make不够聪明,无法分析命令并推断出一次运行规则将构建所有这三个规则. (根据output规则)知道它必须重新构建abc,因此就是这样做的.它对a运行一次规则,对b运行一次规则,对c运行一次.

Your a b c: rule tells Make that this is how to build any of these targets, not how to build all of them. Make is not smart enough to analyze the commands and deduce that running the rule once will build all three. Make knows (from the output rule) that it must rebuild a, b and c, so that's what it does. It runs the first rule once for a, once for b and once for c.

如果要单独重建它们,请执行以下操作:

If you want to rebuild them individually, do this:

a b c:
    echo "Creating $@"
    touch $@

如果要一次全部重建它们,请执行以下操作:

If you want to rebuild them all at once, do something like this:

.PHONY: things
things:
    echo "Creating a b c"
    touch a b c

output: things
    cat a b c > output

或更妙的是:

THINGS = a b c
.PHONY: things
things:
    echo "Creating $(THINGS)"
    touch $(THINGS)

output: things
    cat $(THINGS) > output

这篇关于GNU make:单个规则中有多个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆