1、实战需求
2、工具原料
3、关键技巧
4、规则中的模式替换
5、编译规则依赖
6、小结
7、示例
CC := gcc
MKDIR := mkdir
RM := rm -fr
DIR_OBJS := objs
DIR_TARGET := target
DIRS := $(DIR_OBJS) $(DIR_TARGET)
TARGET := $(DIR_TARGET)/hello-makefile.out
# main.c const.c func.c
SRCS := $(wildcard *.c)
# main.o const.o func.o
OBJS := $(SRCS:.c=.o)
# objs/main.o objs/const.o objs/func.o
OBJS := $(addprefix $(DIR_OBJS)/, $(OBJS))
.PHONY : rebuild clean all
$(TARGET) : $(DIRS) $(OBJS)
$(CC) -o $@ $(OBJS)
@echo "Target File ==> $@"
$(DIRS) :
$(MKDIR) $@
$(DIR_OBJS)/%.o : %.c
ifeq ($(DEBUG),true)
$(CC) -o $@ -g -c $^
else
$(CC) -o $@ -c $^
endif
rebuild : clean all
all : $(TARGET)
clean :
$(RM) $(DIRS)
输出:make
mkdir objs
mkdir target
gcc -o objs/const.o -c const.c
gcc -o objs/main.o -c main.c
gcc -o objs/func.o -c func.c
gcc -o target/hello-makefile.out objs/const.o objs/main.o objs/func.o
Target File ==> target/hello-makefile.out
编译debug调试版本
输出:make DEBUG:=ture
mkdir objs
mkdir target
gcc -o objs/const.o -c const.c
gcc -o objs/main.o -c main.c
gcc -o objs/func.o -c func.c
gcc -o target/hello-makefile.out objs/const.o objs/main.o objs/func.o
Target File ==> target/hello-makefile.out
进入cd target 目录
使用objdump -S hello-makefile.out
查看可执行文件的调试信息
最终目录结构:
---./
-main.c
-func.c
-const.c
-makefile
-objs
-mian.o
-func.o
-const.o
-target
-hello-makefile.out