分享一个通用makefile
程序员文章站
2022-07-02 17:04:43
...
- 适合写一些小程序的时候使用,方便快捷
- c语言 c++ 混编亦可
CROSSCOMPILER :=
EXE := test
MKDIR := mkdir
RM := rm -rf
CC := $(CROSSCOMPILER)gcc
CFLAG :=
CXX := $(CROSSCOMPILER)g++
CXXFLAG :=
LIBS := -lpthread
DIR_OBJS := objs
DIR_EXES := exes
DIR_DEPS := deps
DIRS := $(DIR_OBJS) $(DIR_EXES) $(DIR_DEPS)
EXE := $(addprefix $(DIR_EXES)/, $(EXE))
C_SRCS := $(wildcard *.c)
CXX_SRCS := $(wildcard *.cpp)
OBJS := $(C_SRCS:.c=.o) $(CXX_SRCS:.cpp=.o)
OBJS := $(addprefix $(DIR_OBJS)/, $(OBJS))
DEPS := $(C_SRCS:.c=.dep) $(CXX_SRCS:.cpp=.dep)
DEPS := $(addprefix $(DIR_DEPS)/, $(DEPS))
.PHONY : rebuild clean all
all : $(DIR_OBJS) $(DIR_EXES) $(EXE)
ifeq ("$(MAKECMDGOALS)", "all")
include $(DEPS)
endif
ifeq ("$(MAKECMDGOALS)", "")
include $(DEPS)
endif
$(EXE) : $(OBJS)
ifdef C_SRCS
ifdef CXX_SRCS
$(CXX) -o [email protected] $(OBJS) $(LIBS)
else
$(CC) -o [email protected] $(OBJS) $(LIBS)
endif
else
ifdef CXX_SRCS
$(CXX) -o [email protected] $(OBJS) $(LIBS)
endif
endif
$(DIRS) :
$(MKDIR) [email protected]
$(DIR_OBJS)/%.o : %.c
$(CC) -o [email protected] -c $(filter $(C_SRCS), $^)
$(DIR_OBJS)/%.o : %.cpp
$(CXX) $(CXXFLAG) -o [email protected] -c $(filter $(CXX_SRCS), $^)
ifeq ("$(wildcard $(DIR_DEPS))", "")
$(DIR_DEPS)/%.dep : $(DIR_DEPS) $(C_SRCS) $(CXX_SRCS)
else
$(DIR_DEPS)/%.dep : $(C_SRCS) $(CXX_SRCS)
endif
@echo "Creating [email protected] ..."
@set -e; \
$(CC) -MM -E $(filter $(C_SRCS) $(CXX_SRCS), $^) | sed 's,\(.*\)\.o[ :]*,objs/\1.o [email protected] : ,g' > [email protected]
rebuild : clean all
clean :
$(RM) $(DIRS)
上一篇: VUE中的axios简单使用教程
下一篇: 锚点定位