Skip to content

Instantly share code, notes, and snippets.

@vsee
Created April 8, 2017 07:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsee/227ff0817856420da8636e1a2b8deb97 to your computer and use it in GitHub Desktop.
Save vsee/227ff0817856420da8636e1a2b8deb97 to your computer and use it in GitHub Desktop.
Out of Source Build Makefile Template for C++ Projects
APP=appname
SRC_DIR=src
INC_DIR=inc
OBJ_DIR=obj
BIN_DIR=bin
CC=g++
LD=g++
CFLAGS=-O2 -c -Wall -std=c++11
LFLGAS=
DFLAGS=-g3 -O0 -DDEBUG
INCFLAGS=-I$(INC_DIR)
SOURCES=$(wildcard $(SRC_DIR)/*.cpp)
HEADERS=$(wildcard $(INC_DIR)/*.hpp)
OBJECTS=$(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
DEPENDS=$(OBJ_DIR)/.depends
.PHONY: all
all: $(BIN_DIR)/$(APP)
.PHONY: debug
debug: CFLAGS+=$(DFLAGS)
debug: all
$(BIN_DIR)/$(APP): $(OBJECTS) | $(BIN_DIR)
$(LD) $(LFLGAS) -o $@ $^
$(OBJ_DIR)/%.o: | $(OBJ_DIR)
$(CC) $(CFLAGS) $(INCFLAGS) -o $@ $<
$(DEPENDS): $(SOURCES) | $(OBJ_DIR)
$(CC) $(INCFLAGS) -MM $(SOURCES) | sed -e 's!^!$(OBJ_DIR)/!' >$@
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPENDS)
endif
$(BIN_DIR):
mkdir -p $@
$(OBJ_DIR):
mkdir -p $@
.PHONY: clean
clean:
rm -rf $(BIN_DIR) $(OBJ_DIR)
@vsee
Copy link
Author

vsee commented Apr 8, 2017

Based on a post by elpato.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment