Makefile(二)

Makefile变量

Makefile 变量分为:用户自定义变量、预定义变量、自动变量、环境变量。

在上一篇博客中写的Makefile,其中有很多的内容都重复了,可以使用用户自定义变量给一些内容起个“小名”,重复性的东西调用“小名”就行,既简单又方便。

用户自定义变量定义规则:

VAR = var

Makefile中使用变量格式 $(VAR)

上一篇博客中写的Makefile如下:

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

可以将world定义一个小名 “OBJ”

可以将world.o robot.o human.o animal.o定义一个小名“OBJS”

Makefile中的内容变为如下:

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)


猜你喜欢

转载自blog.csdn.net/weixin_42048417/article/details/80213051