目录
介绍
https://www.cnblogs.com/wittxie/p/9836097.html
上次那个虽然能完成基本的编译功能了,但是还是太low啦,今天来优化一下,找到一篇感觉不错的文章,我们来学习一下,依旧是配合linux源码的makefile框架,只是这次会参考一下上面链接的思想。
其实他用的也是linux的makefile的管理思想编译进行层层调用。
修改前的最新代码和框架
MAJOR = 0
MINOR = 0
PATCH = 0
NAME = tianyu.xin
ROOTFILE = $(shell pwd)
OUTPUTFILE = $(ROOTFILE)/OUTPUT
SRC = $(wildcard *.c)
OBJ = (patsubst %.c,%.o,(SRC))
OUTPUT = hardware_simulation
APP_SRCS = $(wildcard ../APP/src/*.c)
APP_OBJS = $(APP_SRCS:../APP/src/%.c=%.o)
SHOW_SRCS = $(wildcard ../show/src/*.c)
SHOW_OBJS = $(SHOW_SRCS:../show/src/%.c=%.o)
HARDWARE_SRCS = $(wildcard ../hardware/src/*.c)
HARDWARE_OBJS = $(APP_SRCS:../hardware/src/%.c=%.o)
INCLUDE = -I../show/include -I../APP/include -I../hardware/include
CC = gcc
CFLAGS = -Wall -Werror $(INCLUDE)
Aesthetic treatment
log management
ifeq ("$(origin V)", "command line")
KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSE
KBUILD_VERBOSE = 0
endif
ifeq ($(KBUILD_VERBOSE),1)
quiet =
Q =
else
quiet=quiet_
Q = @
endif
all: $(OUTPUT)
(Q)(CURDIR)
$(OUTPUT): $(APP_OBJS) $(SHOW_OBJS)
(Q)(CC) $(CFLAGS) $^ -o $@
%.o: ../APP/src/%.c
(Q)(CC) $(CFLAGS) -c $< -o $@
%.o: ../show/src/%.c
(Q)(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OUTPUT) $(APP_OBJS) $(SHOW_OBJS) $(HARDWARE_OBJS)
FORCE:
$(Q)echo "Forcing target"
.PHONY: FORCE clean all
上面就是目前的makefile
学习和修改
咱们的工程其实不需要那么复杂的编译框架,一个makefile就够了,但是我们要标准化一些,把能拿走的都放到配置文件里。
这个up用的是这种方式,直接把大家的配置文件都放到一起在主makefile那里,
我们就都放到build目录下,一个配置文件,一个共用函数文件
刚刚新加个hardware的rm上面忘记改了直接给我把主函数删了,人麻了,还好vscode有缓存
MAJOR = 0
MINOR = 0
PATCH = 0
NAME = tianyu.xin
include ./make_conf.mk
all: $(OUTPUT)
# $(Q)$(CURDIR)
include ./make_fun.mk
clean:
rm -f $(OUTPUT) $(APP_OBJS) $(SHOW_OBJS) $(HARDWARE_OBJS)
FORCE:
$(Q)echo "Forcing target"
.PHONY: FORCE clean all
修改后makefile文件就变成这样了
这俩makefile的头文件把之前配置的东西都拿走了。
然后试一试昨天加的打印编译过程功能
在试试我新加的功能
我把clean命令拆成了两个全部清除和只清除中间文件
然后我把所有的中间文件和目标文件都放到了OUTPUT目录下
现在代码可以进一步简化了
有的文件单纯就是描述文件没有.o文件所以会导致mv的时候报错
2>/dev/null
可以用这个把标准错误定向到空
现在代码还有个问题就是我们的执行只能在build目录下,所以们在优化一下
ROOT_DIR = ..
OUTPUTFILE = $(ROOT_DIR)/OUTPUT
然后我们再来加个打印头
# Silent printing is always required here
show:
@echo "--------------------------------------------------------------"
@echo "----------------------Build Start-----------------------------"
@echo "--------------------------------------------------------------"
@echo "Version: $(MAJOR).$(MINOR).$(PATCH)\n"
@echo "Author: $(NAME)\n"
还有颜色输出
这个可以参考我之前的一个文章设置对应颜色
ifeq ($(filter debug,$(MAKECMDGOALS)),1)
DEBUG = -g
else
DEBUG =
endif
CC = gcc
CFLAGS = -Wall -Werror $(DEBUG) $(INCLUDE)
现在有一个问题我像上面这样定义没有效果,像下面这样就可以
ifeq ($(filter debug,$(MAKECMDGOALS)),1)
CFLAGS = -Wall -Werror -g $(INCLUDE)
else
CFLAGS = -Wall -Werror $(DEBUG) $(INCLUDE)
endif
CC = gcc
算了debug和waring的开关问题留到下次解决好了
最终版本
MAJOR = 0
MINOR = 0
PATCH = 0
NAME = tianyu.xin
ROOT_DIR = ..
OUTPUTFILE = $(ROOT_DIR)/OUTPUT
include $(ROOT_DIR)/build/make_conf.mk
all: show $(OUTPUT)
$(Q)mkdir -p $(OUTPUTFILE)
$(Q)mv $(OUTPUT) ./*.o $(OUTPUTFILE)
include $(ROOT_DIR)/build/make_fun.mk
# Silent printing is always required here
show:
@echo "--------------------------------------------------------------"
@echo "----------------------Build Start-----------------------------"
@echo "--------------------------------------------------------------"
@echo "Version: $(MAJOR).$(MINOR).$(PATCH)\n"
@echo "Author: $(NAME)\n"
clean:
rm -f $(OUTPUTFILE)/*.o
clean_all:
rm -f $(OUTPUTFILE)/*
FORCE:
$(Q)echo "Forcing target"
.PHONY: FORCE clean all show
SRC = $(wildcard *.c)
OBJ = $(patsubst %.c,%.o,$(SRC))
OUTPUT = hardware_simulation
APP_SRCS = $(wildcard $(ROOT_DIR)/APP/src/*.c)
APP_OBJS = $(APP_SRCS:$(ROOT_DIR)/APP/src/%.c=%.o)
SHOW_SRCS = $(wildcard $(ROOT_DIR)/show/src/*.c)
SHOW_OBJS = $(SHOW_SRCS:$(ROOT_DIR)/show/src/%.c=%.o)
HARDWARE_SRCS = $(wildcard $(ROOT_DIR)/hardware/src/*.c)
HARDWARE_OBJS = $(HARDWARE_SRCS:$(ROOT_DIR)/hardware/src/%.c=%.o)
INCLUDE = -I$(ROOT_DIR)/show/include -I$(ROOT_DIR)/APP/include -I$(ROOT_DIR)/hardware/include
ifeq ($(filter debug,$(MAKECMDGOALS)),1)
DEBUG =
else
DEBUG = -g
endif
CC = gcc
CFLAGS = -Wall -Werror $(INCLUDE)
CFLAGS += $(DEBUG)
# Aesthetic treatment
ECHO_END:=\033[0m"
ECHO_GREEN:=echo "\033[32m
ECHO_RED:=echo "\033[31m
ECHO_YELLOW:=echo "\033[33m
ECHO_BLUE:=echo "\033[34m
ECHO_GREEN_YELLOW:=echo "\033[42;30m
# log management
ifeq ("$(origin V)", "command line")
KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSE
KBUILD_VERBOSE = 0
endif
ifeq ($(KBUILD_VERBOSE),1)
quiet =
Q =
else
quiet=quiet_
Q = @
endif
$(OUTPUT): $(APP_OBJS) $(SHOW_OBJS)
$(Q)$(CC) $(CFLAGS) $^ -o $@
%.o: $(ROOT_DIR)/APP/src/%.c
$(Q)$(CC) $(CFLAGS) -c $< -o $@
%.o: $(ROOT_DIR)/show/src/%.c
$(Q)$(CC) $(CFLAGS) -c $< -o $@