Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Last active August 29, 2015 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Rapptz/eb32184865e435168403 to your computer and use it in GitHub Desktop.
An attempt at a generic Makefile.
# Generic Makefile to help get started
# Requires sh.exe on Windows.
# Assumes GNUMake.
# Made by Rapptz
# name of final output
PROGRAM_NAME := untitled
# where to store the debug and release final output
RELEASE_DIR := ./release
DEBUG_DIR := ./debug
# where to store debug and release object files
RELEASE_OBJ_DIR := ./release/obj
DEBUG_OBJ_DIR := ./debug/obj
# where the source files are
SRC_DIR := .
# compiler name
CXX ?= g++
# general compiler and linker flags
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic
LDFLAGS +=
# debug and release compiler and linker flags
RELEASE_CXXFLAGS := -DNDEBUG -O3
RELEASE_LDFLAGS :=
DEBUG_CXXFLAGS := -DDEBUG -g
DEBUG_LDFLAGS :=
# ignored warnings, emit the -Wno- prefix
IGNORED_WARNINGS :=
# These are flags without the prefixes
# these prefixes are -I, -L, and -l respectively
INCLUDE_DIRS := .
LIBRARY_DIRS :=
LIBRARIES :=
RM ?= rm -rf
MKDIR ?= mkdir -p
RMDIR ?= rmdir
# Input and Output file extension
IN_EXT := cpp
OUT_EXT := o
DEBUG ?= 0
# Modying past this point is usually for internal tinkering.
###############################################################################
ifeq ($(DEBUG), 1)
BIN_DIR := $(DEBUG_DIR)
OBJ_DIR := $(DEBUG_OBJ_DIR)
else
BIN_DIR := $(RELEASE_DIR)
OBJ_DIR := $(RELEASE_OBJ_DIR)
endif
###############################################################################
# Modifying the flags
###############################################################################
CXXFLAGS += $(foreach dir,$(INCLUDE_DIRS),-I$(dir))
CXXFLAGS += $(foreach warn,$(IGNORED_WARNINGS),-Wno-$(warn))
LDFLAGS += $(foreach dir,$(LIBRARY_DIRS),-L$(dir))
LDFLAGS += $(foreach lib,$(LIBRARIES),-l$(lib))
###############################################################################
# Default rules
###############################################################################
all: $(BIN_DIR)/$(PROGRAM_NAME)
clean:
$(RM) $(OBJ_DIR)/*
distclean: clean
uninstall:
-$(RM) $(OBJ_DIR)/* $(BIN_DIR)/*
-$(RMDIR) $(OBJ_DIR)
-$(RMDIR) $(BIN_DIR)
help:
@echo "Available targets: "
@echo "all - builds everything"
@echo "uninstall - removes everything"
@echo "clean - removes object and clean files"
@echo "help - shows this message"
@echo ""
@echo "You can set debug build by doing 'make DEBUG=1 target'"
.PHONY: all clean distclean uninstall help
###############################################################################
# Compiling rules
###############################################################################
SOURCES := $(wildcard $(SRC_DIR)/*.$(IN_EXT))
OBJECTS := $(SOURCES:$(SRC_DIR)/%.$(IN_EXT)=$(OBJ_DIR)/%.$(OUT_EXT))
DEPENDENCIES := ($OBJECTS:.$(OUT_EXT)=.d)
-include $(DEPENDENCIES)
ifeq ($(DEBUG), 1)
CXXFLAGS += $(DEBUG_CXXFLAGS)
LDFLAGS += $(DEBUG_LDFLAGS)
else
CXXFLAGS += $(RELEASE_CXXFLAGS)
LDFLAGS += $(RELEASE_LDFLAGS)
endif
$(OBJ_DIR)/%.$(OUT_EXT):%.$(IN_EXT) | object_dir
$(CXX) -c $(CXXFLAGS) -MMD -o $@ $<
$(BIN_DIR)/$(PROGRAM_NAME): $(OBJECTS) | binary_dir
$(CXX) $(OBJECTS) -o $@ $(LDFLAGS)
object_dir:
ifeq "$(wildcard $(OBJ_DIR))" ""
-$(MKDIR) $(OBJ_DIR)
endif
binary_dir:
ifeq "$(wildcard $(BIN_DIR))" ""
-$(MKDIR) $(BIN_DIR)
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment