Simple understanding of Makefile 3

Simple understanding of Makefile 3

Variables can be used to define the value of variables. There are four ways in the Makefile:
1. Use the'=' operator

/*Makefile程序*/
you = $(me)
me = $(he)
he = she
all:
	echo $(you)
/*END*/

Execution: make -s all
Output: the she
program assigns the value of the variable me to you, and the value of the variable me is the value of the variable he, and the value of he is she, so the value of the variable you is she.
The variable values ​​in the Makefile can be defined using the following variables. But this is dangerous, for example:
a = $(b)
b = $(a)
will fall into the infinite variable assignment process.

2. Use the':=' operator
to define in this way, the previous variable cannot use the latter variable, only the variable that has been defined before can be used. If you use the previously undefined variable,
the value is empty, for example:
y := $(x) me
x := you
all:
echo $(y)

Execution: make -s all
Output: me (Note: If the prompt "missing separator" is prompted, the code is less keyed)
Since the variable x is not defined, it is a null value, and only me is output. Later x has been assigned to you, but y has already been assigned.

3. Use the'?=' operator.
If the variable has not been defined before, the value of the variable is defined at this time. If the value of the variable has been defined, the assignment statement does nothing at this time.
For example:
a := hello
b ?= world
a ?= HELLO
all:
echo $(a)
echo $(b)

Execution: make -s all
output: hello
world,
because the value of a was assigned to hello before, after: a ?= HELLO will no longer assign a value to the variable a, so a keeps the value of hello.

4. Append variable value

Since the variable in the Makefile is essentially a string, the value of the variable can be appended with the'+=' operator. For example:
objects := main.o foo.o
objects += another.o The result is:
objects := main.o foo.o another.o

Guess you like

Origin blog.csdn.net/weixin_46259642/article/details/113567864