Makefile variables
Makefile variables are divided into: user-defined variables, predefined variables, automatic variables, and environment variables.
In the Makefile written in the previous blog, a lot of content is repeated. You can use user-defined variables to give some content a "nickname", and you can call the "nickname" for repetitive things, which is simple and convenient. .
User-defined variable definition rules:
VAR = var
Use variable format $(VAR) in Makefile
The Makefile written in the previous blog is as follows:
world: world.orobot.o human.o animal.o
gccworld.o robot.o human.o animal.o -o world
animal.o: animal.c
gccanimal.c -c -o animal.o
human.o: human.c
gcchuman.c -c -o human.o
robot.o: robot.c
gccrobot.c -c -o robot.o
world.o: world.c
gccworld.c -c -o world.o
clean:
worldworld.o robot.o human.o animal.o
The world can be defined as a nickname "OBJ"
You can define a nickname "OBJS" for world.o robot.o human.o animal.o
The contents of the Makefile become as follows:
OBJ = world
OBJS = world.o robot.o human.o animal.o
$ (OBJ): $ (OBJS)
gcc$(OBJS) -o $(OBJ)
animal.o: animal.c
gccanimal.c -c -o animal.o
human.o: human.c
gcchuman.c -c -o human.o
robot.o: robot.c
gccrobot.c -c -o robot.o
world.o: world.c
gccworld.c -c -o world.o
clean:
rm $ (OBJ) $ (OBJS)