linux简单的通用Makefile模板

2019-07-13 05:19发布

供写小程序的时候使用 可以随意的修改文件名称 随意的增删文件、目录 随意的调整文件的目录层次 不编译某个.c文件时将其改个后缀名即可 不能有重复的文件名称   # wugangnan@hcn2000.com CROSS_COMPILE= /opt/poky/1.7.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-linux-gnueabihf- CC = $(CROSS_COMPILE)arm-linux-androideabi-gcc AS = as LD = ld #CC = gcc CPP = $(CC) -E AR = ar NM = nm # 程序的默认名称 TARGET = xf6000test # 命令行中使用p=X或P=X修改默认程序名称 ifeq ("$(origin p)", "command line") TARGET = $(p) endif ifeq ("$(origin P)", "command line") TARGET = $(P) endif DIRS = $(shell find . -type d) # 找出所有的.c .h .a .so文件及目录 CFILES_DIR = $(shell find . -type f -name "*.c") CFILES = $(notdir $(CFILES_DIR)) HFILES_DIR = $(shell find . -type f -name "*.h") HFILES = $(notdir $(HFILES_DIR)) HDIRS = $(sort $(dir $(HFILES_DIR))) AFILES_DIR = $(shell find . -type f -name "lib*.a") AFILES = $(notdir $(AFILES_DIR)) ADIRS = $(sort $(dir $(AFILES_DIR))) SOFILES_DIR = $(shell find . -type f -name "lib*.so") SOFILES = $(notdir $(SOFILES_DIR)) SODIRS = $(sort $(dir $(SOFILES_DIR))) # 包含所有含有.h文件的目录 INCLUDES += $(HDIRS:%=-I%) $(warning INCLUDES : [ $(INCLUDES) ]) CFLAGS += $(INCLUDES) # 编译标志 CFLAGS += -Wall -Werror CFLAGS += -g -MD -O2 -static # 含有.a .so文件的目录 LDFLAGS += $(ADIRS:%=-L%) LDFLAGS += $(SODIRS:%=-L%) # 引用库文件 LDFLAGS += $(AFILES:lib%.a=-l%) LDFLAGS += $(SOFILES:lib%.so=-l%) LDFLAGS += -lpthread $(warning LDFLAGS : [ $(LDFLAGS) ]) # 包含所有的目录 VPATH = $(DIRS) # 所有的.c文件 SOURCES = $(CFILES) $(warning SOURCES : [ $(SOURCES) ]) # 目标及依赖 OBJS = $(SOURCES:%.c=obj/%.o) DEPS = $(SOURCES:%.c=obj/%.d) ########################################################### .PHONY : all clean cleanall all : $(TARGET) $(TARGET) : $(OBJS) @echo @echo "Linking..." @echo $(CC) $(LDFLAGS) $^ -o $@ @echo @echo "enjoy < $(TARGET) > Good Luck." @echo obj/%.o : %.c @mkdir -p obj $(CC) $(CFLAGS) -c $< -o $@ clean : rm -rf $(OBJS) $(DEPS) obj cleanall : rm -rf $(OBJS) $(DEPS) obj $(TARGET) # 包含生成的依赖关系 sinclude $(DEPS)