Last active
November 16, 2019 06:09
-
-
Save leetking/90f4b2a41e35a03deb18897eb2f771d5 to your computer and use it in GitHub Desktop.
the template file for C/C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP := <App Name> | |
VER := <Version> | |
PKG = $(APP)-$(VER) | |
DEBUG := True | |
CC := gcc | |
CXX := g++ | |
RM := rm -rf | |
MKDIR := mkdir -p | |
CSRCS := $(sort $(wildcard *.c)) | |
COBJS := $(CSRCS:.c=.c.o) | |
CDEPS := $(CSRCS:.c=.c.d) | |
CXXSRCS := $(sort $(wildcard *.cc)) | |
CXXOBJS := $(CXXSRCS:.cc=.cc.o) | |
CXXDEPS := $(CXXSRCS:.cc=.cc.d) | |
THIRD_LIBS := | |
CFLAGS := $(foreach dep,$(THIRD_LIBS),$(shell pkg-config --cflags $(dep))) \ | |
-std=c99 -MMD -MP -Wno-unused-result | |
CXXFLAGS := $(foreach dep,$(THIRD_LIBS),$(shell pkg-config --cflags $(dep))) \ | |
-std=c++11 -MMD -MP | |
LDFLAGS := $(foreach dep,$(THIRD_LIBS),$(shell pkg-config --libs $(dep))) | |
CFLAGS_DEBUG := -g3 -O0 -DDEBUG -Wall -Wno-unused -Wno-format-truncation \ | |
-Wformat -fsanitize=address | |
CXXFLAGS_DEBUG := $(CFLAGS_DEBUG) | |
LDFLAGS_DEBUG := -fsanitize=address -rdynamic | |
CFLAGS_RELEASE := -O2 -DNDEBUG -g -Wall -Wno-format-truncation -Werror -Wformat | |
CXXFLAGS_RELEASE := $(CFLAGS_RELEASE) | |
LDFLAGS_RELEASE := | |
ifeq ($(DEBUG), True) | |
CFLAGS += $(CFLAGS_DEBUG) | |
CXXFLAGS += $(CXXFLAGS_DEBUG) | |
LDFLAGS += $(LDFLAGS_DEBUG) | |
else | |
CFLAGS += $(CFLAGS_RELEASE) | |
CXXFLAGS += $(CXXFLAGS_RELEASE) | |
LDFLAGS += $(LDFLAGS_RELEASE) | |
endif | |
$(APP): $(COBJS) $(CXXOBJS) | |
@$(CXX) -o $@ $^ $(LDFLAGS) | |
@echo "GEN $@" | |
%.c.o: %.c | |
@$(CC) -o $@ -c $< $(CFLAGS) | |
@echo "CC $@" | |
%.cc.o: %.cc | |
@$(CXX) -o $@ -c $< $(CXXFLAGS) | |
@echo "CXX $@" | |
-include $(CDEPS) | |
-include $(CXXDEPS) | |
define rm | |
@$(RM) $(1) | |
@echo "RM $(1)" | |
endef | |
clean: | |
$(foreach f,$(sort $(COBJS) $(CXXOBJS) $(APP)),$(call rm,$(f))) | |
distclean: clean | |
$(foreach f,$(sort $(CDEPS) $(CXXDEPS)),$(call rm,$(f))) | |
$(call rm,"compile_commands.json") | |
tar: | |
.PHONY: clean distclean tar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP := <App Name> | |
VER := <Version> | |
# 不立即展开 $(APP) 和 $(VER) | |
PKG = $(APP)-$(VER) | |
DEBUG := True | |
CC := gcc | |
RM := rm -rf | |
CP := cp -r | |
MKDIR := mkdir -p | |
SRCS := $(sort $(wildcard *.c)) | |
DEPS := $(SRCS:.c=.c.d) | |
OBJS := $(SRCS:.c=.c.o) | |
THIRD_LIBS := | |
CFLAGS := $(foreach dep,$(THIRD_LIBS),$(shell pkg-config --cflags $(dep))) \ | |
-std=c99 -MMD -MP -Wno-unused-result | |
LDFLAGS := $(foreach dep,$(THIRD_LIBS),$(shell pkg-config --libs $(dep))) | |
CFLAGS_DEBUG := -g3 -O0 -DDEBUG -Wall -Wno-unused -Wno-format-truncation \ | |
-Wformat -fsanitize=address | |
LDFLAGS_DEBUG := -fsanitize=address -rdynamic | |
CFLAGS_RELEASE := -O2 -DNDEBUG -g -Wall -Wno-format-truncation -Werror -Wformat | |
LDFLAGS_RELEASE := | |
ifeq ($(DEBUG), True) | |
CFLAGS += $(CFLAGS_DEBUG) | |
LDFLAGS += $(LDFLAGS_DEBUG) | |
else | |
CFLAGS += $(CFLAGS_RELEASE) | |
LDFLAGS += $(LDFLAGS_RELEASE) | |
endif | |
$(APP): $(OBJS) | |
@$(CC) -o $@ $^ $(LDFLAGS) | |
@echo "GEN $@" | |
%.c.o: %.c | |
@$(CC) -o $@ -c $< $(CFLAGS) | |
@echo "CC $@" | |
-include $(DEPS) | |
define rm | |
@$(RM) $(1) | |
@echo "RM $(1)" | |
endef | |
clean: | |
$(foreach f,$(sort $(OBJS) $(APP)),$(call rm,$(f))) | |
distclean: clean | |
$(foreach f,$(sort $(DEPS)),$(call rm,$(f))) | |
$(call rm,"compile_commands.json") | |
tar: | |
.PHONY: clean distclean tar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment