Makefile模板

具体Makefile语法就不多说了,请看源码就明白了
该Makefile在Linux、Windows平台都可以使用,交叉编译只需要设置CROSS_COMPILE变量即可

#+++++++++++++++++++++++++++++++++configure++++++++++++++++++++++++++++++++++++++++
# OS=Windows,Linux,Android
# ARCH=x86,x86_64,arm,aarch64
# CC  = $(CROSS_COMPILE)gcc
# CXX = $(CROSS_COMPILE)g++
# CPPFLAGS += $(addprefix -D, $(DEFINES))
# CPPFLAGS += $(addprefix -I, $(INCDIRS))
# LDFLAGS  += $(addprefix -L, $(LIBDIRS))
# LDFLAGS  += $(addprefix -l, $(LIBS))
#
# Usage:
# make all TARGET=libxx TARGET_TYPE=SHARED \
# CROSS_COMPILE=arm-linux-androideabi- \
# DEFINES=USE_OPENCV \
# LIBS="opencv_core opencv_highgui"
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# VARIABLES
TARGET=test
#BUILD_TYPE=DEBUG,RELEASE
BUILD_TYPE=DEBUG
#TARGET_TYPE=EXECUTABLE,SHARED,STATIC
TARGET_TYPE=EXECUTABLE

# COMMANDS
CC 	= $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
CPP = $(CC) -E
AS 	= $(CROSS_COMPILE)as
LD	= $(CROSS_COMPILE)ld
AR	= $(CROSS_COMPILE)ar
NM	= $(CROSS_COMPILE)nm
STRIP 	= $(CROSS_COMPILE)strip
OBJCOPY	= $(CROSS_COMPILE)objcopy
OBJDUMP	= $(CROSS_COMPILE)objdump

MKDIR = -mkdir -p
CP = -cp -r
RM = -rm -r

# PLATFORM: OS, ARCH
CC_VERSION=$(shell $(CC) --version 2>&1 | head -n 1)
TARGET_PLATFORM=$(shell $(CC) -v 2>&1 | grep Target | sed 's/Target: //')
ifneq ($(findstring mingw, $(TARGET_PLATFORM)), )
	OS=Windows
endif
ifneq ($(findstring android, $(TARGET_PLATFORM)), )
	OS=Android
endif
ifndef OS
	OS=Linux
endif

ifndef ARCH
ARCH=$(shell echo $(TARGET_PLATFORM) | awk -F'-' '{print $$1}')
endif

# CFLAGS, CXXFLAGS, ARFLAGS
ifeq ($(BUILD_TYPE), DEBUG)
	DEFAULT_CFLAGS = -g
endif
DEFAULT_CFLAGS += -Wall -O3 -fPIC
ifndef CFLAGS
CFLAGS := $(DEFAULT_CFLAGS) -std=c99 $(MAKE_CFLAGS)
endif
ifndef CXXFLAGS
CXXFLAGS := $(DEFAULT_CFLAGS) -std=c++11 $(MAKE_CXXFLAGS)
endif
ifndef ARFLAGS
ARFLAGS := cr
endif

# DIRS
PREFIX = install
ifeq ($(OS), Linux)
	PREFIX = /usr/local
endif
INCDIR = include
LIBDIR = lib
SRCDIR = src
BINDIR = bin
DEPDIR = 3rd
CONFDIR = etc
DISTDIR = dist
DOCDIR  = doc

SRCDIRS += $(shell find $(SRCDIR) -type d)
INCDIRS += $(INCDIR) $(DEPDIR) $(DEPDIR)/include $(SRCDIRS)
LIBDIRS += $(LIBDIR) $(DEPDIR)/lib $(DEPDIR)/lib/$(TARGET_PLATFORM)

# CPPFLAGS
ifeq ($(OS), Windows)
	CPPFLAGS += -D_WIN32_WINNT=0x600
ifeq ($(TARGET_TYPE), SHARED)
	CPPFLAGS += -DDLL_EXPORTS
endif
endif
ifeq ($(BUILD_TYPE), DEBUG)
	CPPFLAGS += -DDEBUG
endif
CPPFLAGS += $(addprefix -D, $(DEFINES))
CPPFLAGS += $(addprefix -I, $(INCDIRS))

# LDFLAGS
ifeq ($(OS), Windows)
	LDFLAGS += -static-libgcc -static-libstdc++
endif
LDFLAGS += $(addprefix -L, $(LIBDIRS))
LDFLAGS += $(addprefix -l, $(LIBS))

ifeq ($(OS), Windows)
	LDFLAGS += -Wl,-Bdynamic -lwinmm -liphlpapi -lws2_32
	LDFLAGS += -Wl,-Bstatic -lstdc++ -lpthread -lm
else
ifeq ($(OS), Android)
	LDFLAGS += -Wl,-Bdynamic -lstdc++ -lm -llog
else
	LDFLAGS += -lstdc++ -lpthread -lm
endif
endif

SRCS += $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c $(dir)/*.cc $(dir)/*.cpp))
ifeq ($(SRCS), )
	SRCS = $(wildcard *.c *.cc *.cpp)
endif
#OBJS += $(patsubst %.c, %.o, $(SRCS))
#OBJS += $(patsubst %.cc, %.o, $(SRCS))
#OBJS += $(patsubst %.cpp, %.o, $(SRCS))
OBJS := $(addsuffix .o, $(basename $(SRCS)))

INSTALLED_INCS=$(addprefix $(PREFIX)/$(INCDIR)/, $(shell ls $(INCDIR)))
INSTALLED_LIBS=$(addprefix $(PREFIX)/$(LIBDIR)/, $(shell ls $(LIBDIR)))
INSTALLED_BINS=$(addprefix $(PREFIX)/$(BINDIR)/, $(shell ls $(BINDIR)))

# info
$(info $(CC_VERSION))

$(info OS   = $(OS))
$(info ARCH = $(ARCH))
$(info MAKE = $(MAKE))
$(info CC   = $(CC))
$(info CXX  = $(CXX))

$(info CFLAGS   = $(CFLAGS))
$(info CXXFLAGS = $(CXXFLAGS))
$(info CPPFLAGS = $(CPPFLAGS))
$(info LDFLAGS  = $(LDFLAGS))

$(info TARGET           = $(TARGET))
$(info TARGET_TYPE      = $(TARGET_TYPE))
$(info TARGET_PLATFORM  = $(TARGET_PLATFORM))
$(info BUILD_TYPE       = $(BUILD_TYPE))

$(info SRCS=$(SRCS))
$(info OBJS=$(OBJS))

$(info INSTALLED_INCS=$(INSTALLED_INCS))
$(info INSTALLED_LIBS=$(INSTALLED_LIBS))
$(info INSTALLED_BINS=$(INSTALLED_BINS))

default: all

all: prepare $(TARGET)

prepare:
	$(MKDIR) $(BINDIR) $(LIBDIR)

$(TARGET): $(OBJS)
ifeq ($(TARGET_TYPE), SHARED)
ifeq ($(OS), Windows)
	$(CC) -shared $^ -o $(LIBDIR)/[email protected] $(LDFLAGS) -Wl,--output-def,$(LIBDIR)/$(@).def
else
	$(CC) -shared $^ -o $(LIBDIR)/[email protected] $(LDFLAGS)
endif
else
ifeq ($(TARGET_TYPE), STATIC)
	$(AR) $(ARFLAGS) $(LIBDIR)/[email protected] $^
else
ifeq ($(OS), Windows)
	$(CC) $^ -o $(BINDIR)/[email protected] $(LDFLAGS)
else
	$(CC) $^ -o $(BINDIR)/$@ $(LDFLAGS)
endif
endif
endif

clean:
	$(RM) $(OBJS)
	$(RM) $(LIBDIR)
	$(RM) $(BINDIR)

install:
	$(CP) $(INCDIR)/* $(PREFIX)/$(INCDIR)/
	$(CP) $(LIBDIR)/* $(PREFIX)/$(LIBDIR)/
	$(CP) $(BINDIR)/* $(PREFIX)/$(BINDIR)/

uninstall:
	$(RM) $(INSTALLED_INCS)
	$(RM) $(INSTALLED_LIBS)
	$(RM) $(INSTALLED_BINS)

dist:
	$(MKDIR) $(DISTDIR)
	$(CP) $(INCDIR) $(LIBDIR) $(BINDIR) $(CONFDIR) $(DOCDIR) $(DISTDIR)

undist:
	$(RM) $(DISTDIR)

.PHONY: default all prepare clean install uninstall dist undist

你可能感兴趣的:(c/c++)