通用makefile模板,跨平台,适合大小工程

跨平台的通用makefile模板

同时兼容C, C++, ASM文件编译
支持MCU嵌入式, windows, linux平台的gcc/g++编译
支持更新头文件后, 自动编译依赖头文件的相关文件

测试环境:
  Linux:vscode + gcc
  windows:mingw64 + bash (git自带)
    由于含LINUX命令行, windows下使用git自带的bash命令行工具(此工具包含多数常用LINUX命令)

本模板包含:mkenv.mk(和运行平台相关的配置) + Makefile(与平台无关的工程配置)

修改mkenv.mk:
  编译不同平台的目标, 修改编译环境

修改makefile:
  添加头文件路径, 库, 源文件, 宏定义

如有需要完善的地方可留言

-------------------Makefile文件:------------------------

include mkenv.mk
 
####################工程配置, 配置工程一般只需修改这里########################
#define宏定义 -DXXX
DEFINES	+= #-D__ARM32_ARCH__=5
 
#include path: -I头文件路径
INCDIRS	+= #-Istm32/include
 
#library path
LIBDIRS	+=
 
#library: 库全称为libXXX.a, 添加时: -lXXX
LIBS += -lgcc
 
#c source path: 添加目录后, 自动搜查此目录的.c文件
SRCDIRS_C += 
SRC_C += $(wildcard $(foreach n, $(SRCDIRS_C), $(n)/*.c))
 
#c source files: 手动添加.c文件, 文件前要吧包括路径
SRC_C += main.c
 
#c++ source path
SRCDIRS_CXX += 
SRC_CXX += $(wildcard $(foreach n, $(SRCDIRS_CXX), $(n)/*.cpp))
 
#c++ source files
SRC_CXX += 
 
#asm source files
SRC_ASM += 
###################################################################
 
###############下面为编译规则(正常不用管)#################
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(addprefix $(BUILD)/, $(SRC_ASM:.S=.o)) $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
 
#添加词句后, 更新头文件后会更新依赖头文件的相关文件, (.d文件在编译时生成)
-include $(addprefix $(BUILD)/, $(SRC_C:.c=.d)) $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.d))
 
define compile_c
$(ECHO) "CC $<"
$(Q)$(CC) $(INCDIRS) $(MCFLAGS) $(CFLAGS) -c -MD -o $@ $<
@# The following fixes the dependency file.
@# See http://make.paulandlesley.org/autodep.html for details.
@# Regex adjusted from the above to play better with Windows paths, etc.
@$(CP) $(@:.o=.d) $(@:.o=.P); \
  $(SED) -e 's/#.*//' -e 's/^.*:  *//' -e 's/ *\\$$//' \
      -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P);
endef
 
define compile_cxx
$(ECHO) "CXX $<"
$(Q)$(CXX) $(INCDIRS) $(MCFLAGS) $(CXXFLAGS) -c -MD -o $@ $<
@# The following fixes the dependency file.
@# See http://make.paulandlesley.org/autodep.html for details.
@# Regex adjusted from the above to play better with Windows paths, etc.
@$(CP) $(@:.o=.d) $(@:.o=.P); \
  $(SED) -e 's/#.*//' -e 's/^.*:  *//' -e 's/ *\\$$//' \
      -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P);
endef
 
$(BUILD)/%.o: %.c
	$(call compile_c)
 
$(BUILD)/%.o: %.cpp
	$(call compile_cxx)
 
$(BUILD)/%.o: %.S
	$(ECHO) "AS $<"
	$(Q)$(AS) $(MCFLAGS) $(ASFLAGS) -c -o $@ $<
	
$(BUILD)/%.o: %.s
	$(ECHO) "AS $<"
	$(Q)$(AS) $(MCFLAGS) $(CFLAGS) -o $@ $<
 
OBJ_DIRS = $(sort $(dir $(OBJ)))
$(OBJ): | $(OBJ_DIRS)
$(OBJ_DIRS):
	$(MKDIR) -p $@
 
$(BUILD)/$(addprefix $(PROG), $(EXTENSION)): $(OBJ)
	$(ECHO) "LINK $@"
	$(Q)$(CC) $(LDFLAGS) -Wl,--cref,-Map=$@.map -o $@ $^ $(LIBS)
	$(Q)$(SIZE) $@
 
$(BUILD)/$(addprefix $(PROG), .bin): $(BUILD)/$(addprefix $(PROG), $(EXTENSION))
	$(Q)$(OBJCOPY) -v -O binary $^ $@
	$(ECHO) Make header information for brom booting

----------------------mkenv.mk文件:------------------------------------

#平台make环境设置文件
 
.PHONY:all clean test_makefile
.PHONY:write mktool dump run
 
#开启makefile规则打印
BUILD_VERBOSE = 0
ifeq ($(BUILD_VERBOSE),0)
Q = @
else
Q =
endif
 
# define main target
PROG ?= APP
 
# system app extension
EXTENSION ?= .exe
 
# define output dir
BUILD ?= build
 
# define command
RM = rm
ECHO = @echo
CP = cp
MKDIR = mkdir
SED = sed
PYTHON = python
 
#gcc compiler select
CROSS_COMPILE ?= #arm-eabi-
CC	          = $(CROSS_COMPILE)gcc
AS	          = $(CROSS_COMPILE)gcc -x assembler-with-cpp
CXX 		  = $(CROSS_COMPILE)g++
GDB 		  = $(CROSS_COMPILE)gdb
LD	          = $(CROSS_COMPILE)ld
OBJCOPY	      = $(CROSS_COMPILE)objcopy
OBJDUMP       = $(CROSS_COMPILE)objdump
SIZE 		  = $(CROSS_COMPILE)size
STRIP 		  = $(CROSS_COMPILE)strip
AR 			  = $(CROSS_COMPILE)ar
 
#define 编译器选项
DEFINES		+= 
 
# compiler set
# Tune for Debugging or Optimization
DEBUG ?= 0
ifeq ($(DEBUG), 1)
PROG = main_debug
ASFLAGS		= -g -ggdb -Wall -O0 -ffreestanding -std=gnu99 $(DEFINES)
CFLAGS		= -g -ggdb -Wall -O0 -ffreestanding -std=gnu99 $(DEFINES)
CXXFLAGS	= -g -ggdb -Wall -O0 -ffreestanding -std=c++11 $(DEFINES)
else
PROG = main_release
ASFLAGS		= -Wall -O3 -ffreestanding -std=gnu99 $(DEFINES)
CFLAGS		= -Wall -O3 -ffreestanding -std=gnu99 $(DEFINES)
CXXFLAGS	= -Wall -O3 -ffreestanding -std=c++11 $(DEFINES)
endif
 
#链接文件-嵌入式使用
LDFLAGS		= 
#编译器选项
MCFLAGS		= 
 
 
all: $(BUILD)/$(addprefix $(PROG), $(EXTENSION))
	$(ECHO) "<><><><><><>><><>><><><><><><><><><>><><><><><><>"
	$(ECHO) make compile ok: $< 
	$(ECHO) $(shell date +%F%n%T)
	$(ECHO) "<><><><><><>><><>><><><><><><><><><><><><><<><><>"
 
run:
	$(ECHO) run ./$(BUILD)/$(addprefix $(PROG), $(EXTENSION))
	@ ./$(BUILD)/$(addprefix $(PROG), $(EXTENSION))
 
clean:
	find ./$(BUILD)/ -name "*.o"  | xargs rm -f  
	find ./$(BUILD)/ -name "*.bin"  | xargs rm -f
	find ./$(BUILD)/ -name "*.elf"  | xargs rm -f
	find ./$(BUILD)/ -name "*.P"  | xargs rm -f
	find ./$(BUILD)/ -name "*.d"  | xargs rm -f
	find ./$(BUILD)/ -name "*.map"  | xargs rm -f
 
dump:
	$(OBJDUMP) -S app.o | less
write:
	sudo fileware_download $(BUILD)/$(addprefix $(PROG), .bin)
test_makefile:
	@echo $(SRC_C)

原文出处链接和本声明:https://blog.csdn.net/Saoskywalker/article/details/118097779

你可能感兴趣的:(经验分享,linux,bash,c语言)