Jame H Jame H - 1 month ago 5
C Question

Logic of breakoutline in Makefile depedencies

I'm reading a C library. And i met a strange makefile types with multi depedencies on multi lines, such as:

phuonga:
echo ">>>>>>>>>>>phuong a"
.PHONY : phuonga

phuongb:
echo ">>>>>>>>>>>phuong b"
.PHONY : phuongb
phuongc:
echo ">>>>>>>>>>>phuong c"
.PHONY : phuongc

phuong: phuonga
phuong: phuongb
phuong: phuongc
echo ">>>>>> Runned phuong"


But when i run by make phuong, this result is:

>>>>>>>>>>>phuong c
>>>>>>>>>>>phuong a
>>>>>>>>>>>phuong b
>>>>>> Runned phuong


It's not appear a,b,c or c,b,a. The order is c,a,b
Why it follows this order? I don't understand that logic.

Thank a lot

Answer

The first prerequisites are always the ones in the prerequisite list of the rule containing the recipe. After that the prerequisites are added in the order that they were seen in the makefile. So for:

phuong: phuonga
phuong: phuongb
phuong: phuongc
        echo ">>>>>> Runned phuong"

the line containing the recipe lists phuongc as the prerequisite so that always comes first. After that the first prerequisite seen is phuonga so that comes next, followed by phuongb.

If you write the rules like this:

phuong: phuonga
phuong: phuongb
phuong: phuongc
phuong:
        echo ">>>>>> Runned phuong"

where none of the prerequisites are listed in the rule containing the recipe, then they'll come out in the order they are defined in the makefile (phuonga, phuongb, phuongc).

Comments