简体   繁体   English

makefile 执行顺序是什么

[英]what is makefile executing order

I have following makefile code.我有以下 makefile 代码。

objects = foo.o all.o bar.o 
all: $(objects)

foo.o: foo.c
all.o: all.c
bar.o: bar.c

%.c:
    touch $@

all.c:
    echo "int main() { return 0; }" > all.c

clean:
    rm -f *.c *.o all

the result of the make is制作的结果是

echo "int main() { return 0; }" > all.c
cc    -c -o all.o all.c
touch foo.c
cc    -c -o foo.o foo.c
touch bar.c
cc    -c -o bar.o bar.c
cc   all.o foo.o bar.o   -o all

I am confused what is the executing order of the makefile?我很困惑makefile的执行顺序是什么?

  1. why all.c is executing first?为什么 all.c 先执行?
  2. followed by all.o?其次是all.o?
  3. last one is cc all.o foo.o bar.o -o all, where it comes from?最后一个是cc all.o foo.o bar.o -o all,它是从哪里来的?

thanks a lot多谢

Well, there is a defined order that make will use to investigate which targets to be built.嗯,有一个定义的顺序,make 将使用它来调查要构建的目标。 Make will start with the goal target, which is either (a) a target you gave it on the command line, or (b) the first explicit target in your makefile if you didn't give a target on the command line. Make 将从目标目标开始,该目标是 (a) 您在命令行中指定的目标,或者 (b) 如果您没有在命令行中指定目标,则 makefile 中的第一个显式目标。 For each target make wants to build, it considers each prerequisite in order as if it were a target, looks at its prerequisites, etc.对于 make 想要构建的每个目标,它会按顺序考虑每个先决条件,就好像是一个目标一样,查看它的先决条件等。

The important thing to know is that if you don't define a recipe, make will try to find a recipe on its own by using an implicit rule (a pattern rule for example).要知道的重要一点是,如果您不定义配方,make 将尝试使用隐式规则(例如模式规则)自行查找配方。 And, the implicit rule might have its OWN prerequisites and those are considered FIRST, before any prerequisites you add.并且,隐式规则可能有其 OWN 先决条件,并且在您添加任何先决条件之前,这些先决条件被视为第一。

Here you have this as the first explicit rule:在这里,您将其作为第一个显式规则:

all: $(objects)

There is no recipe here so make will try to find one.这里没有食谱,所以 make 会尝试找到一个。 Make knows how to build an executable from a source file; Make 知道如何从源文件构建可执行文件; it has a built-in rule like this:它有一个这样的内置规则:

% : %.c ; ...

Your makefile has a rule to build all.c , so this pattern rule matches, which means after the rule is found what make really sees is this:你的 makefile 有一个规则来构建all.c ,所以这个模式规则匹配,这意味着在找到规则之后,make真正看到的是:

all : all.c foo.o all.o bar.o
        ...

Now you can probably work out why you get the output you see.现在您可能会弄清楚为什么会得到您看到的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM