通用Makefile和Makefile.build
通用Makefile
通用的Makefile赠送给大家!
Makefile:
******************************************************************************************
CROSS_COMPILE = arm-none-linux-gnueabi-
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
# export导出的变量是给子目录下的Makefile使用的
export AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP
# 编译器在编译时的参数设置
CFLAGS := -Wall -O2 -g
# 添加头文件路径,不添加的话include目录下的头文件编译时找不到
CFLAGS += -I $(shell pwd)/include
# 链接器的链接参数设置
LDFLAGS :=
export CFLAGS LDFLAGS
TOPDIR := $(shell pwd)
export TOPDIR
# 定义将来编译生成的可执行程序的名字
TARGET := imageplayer
# 添加项目中所有用到的源文件,有顶层目录下的.c文件,和子文件夹
# 添加顶层目录下的.c文件
obj-y += main.o
obj-y += a/
obj-y += b/
obj-y += c/
# 添加顶层目录下的子文件夹(注意目录名后面加一个/)
all:
make -C ./ -f $(TOPDIR)/Makefile.build
$(CC) $(LDFLAGS) -o $(TARGET) built-in.o
cp:
cp ../testproject/ /root/rootfs/ -rf
clean:
rm -f $(shell find -name "*.o")
rm -f $(TARGET)
distclean:
rm -f $(shell find -name "*.o")
rm -f $(shell find -name "*.d")
rm -f $(TARGET)
******************************************************************************************
Makefile.build
******************************************************************************************
PHONY := __build
__build:
obj-y :=
subdir-y :=
include Makefile
__subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) #a b c
subdir-y += $(__subdir-y) #a b c
subdir_objs := $(foreach f,$(subdir-y),$(f)/built-in.o) #a/built-in.o b/built-in.o c/built-in.o
cur_objs := $(filter-out %/, $(obj-y)) #main.o
dep_files := $(foreach f,$(cur_objs),.$(f).d) #main.o.d
dep_files := $(wildcard $(dep_files))
ifneq ($(dep_files),)
include $(dep_files)
endif
PHONY += $(subdir-y) # a b c
__build : $(subdir-y) built-in.o
$(subdir-y):
make -C $@ -f $(TOPDIR)/Makefile.build
built-in.o : $(cur_objs) $(subdir_objs)
$(LD) -r -o $@ $^
dep_file = .$@.d
%.o : %.c
$(CC) $(CFLAGS) -Wp,-MD,$(dep_file) -c -o $@ $<
.PHONY : $(PHONY)
******************************************************************************************