Created
December 16, 2014 21:42
-
-
Save robertrueger/e5546fefc25a90992ff8 to your computer and use it in GitHub Desktop.
A drop-in makefile for lazy C++ programmers
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
# disable default implicit rules | |
MAKEFLAGS += --no-builtin-rules | |
.SUFFIXES: | |
# project name | |
PROJECT = myproject | |
# common compiler/linker flags | |
CXX = g++ | |
CXXFLAGS = -std=c++11 -Wall -Wextra -Wpedantic | |
LDFLAGS = | |
DEFINES = | |
# build specific compiler/linker flags | |
BUILD ?= release | |
ifeq ($(BUILD), debug) | |
CXXFLAGS += -Og -g | |
DEFINES += -DVERBOSE=1 | |
else ifeq ($(BUILD), profile) | |
CXXFLAGS += -march=native -O3 -g | |
DEFINES += -DNDEBUG | |
else ifeq ($(BUILD), release) | |
CXXFLAGS += -march=native -O3 -flto | |
LDFLAGS += -fuse-linker-plugin -s | |
DEFINES += -DNDEBUG | |
else | |
$(error unrecognized BUILD) | |
endif | |
BUILDDIR = bin.$(BUILD) | |
# find the hash of the git commit we are building from | |
GIT_HASH = $(shell git rev-parse --short HEAD 2> /dev/null || echo *unknown*) | |
DEFINES += -DGIT_HASH=\"$(GIT_HASH)\" | |
# generate lists of object files (all and existing only) | |
OBJECTS_ALL = $(addprefix $(BUILDDIR)/,$(patsubst %.cpp,%.o,$(wildcard *.cpp))) | |
OBJECTS_EXT = $(wildcard $(BUILDDIR)/*.o) | |
# define implicit rule to compile a cpp file | |
$(BUILDDIR)/%.o : %.cpp | $(BUILDDIR) | |
$(CXX) $(CXXFLAGS) $(DEFINES) -c $< -o $@ | |
$(CXX) $(CXXFLAGS) $(DEFINES) -MM $< -MT $@ > $(BUILDDIR)/$*.dep | |
# main executable | |
$(BUILDDIR)/$(PROJECT) : $(OBJECTS_ALL) | $(BUILDDIR) | |
$(CXX) $(CXXFLAGS) $^ $(LDFLAGS) -o $@ | |
# build directory creation | |
$(BUILDDIR) : | |
mkdir $(BUILDDIR) | |
# pull in dependency info for existing object files | |
-include $(OBJECTS_EXT:.o=.dep) | |
# obligatory clean target | |
clean : | |
rm -rf bin.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment