Skip to content

Instantly share code, notes, and snippets.

@pagdot

pagdot/Makefile

Created May 31, 2020
Embed
What would you like to do?
Basic makefile
BIN_NAME=hello
# Compile and link flags
DEFINES=
#-pedantic treats warning as errors
CPPFLAGS=-Wall -Werror -pedantic -O2 -g $(addprefix -D,$(DEFINES))
LDFLAGS=
# Directory constants
BIN_DIR=bin
SRC_DIR=src
OBJ_DIR=obj
# Source and object files
BINARY=$(BIN_DIR)/$(BIN_NAME)
SOURCES=$(wildcard $(SRC_DIR)/*.cpp)
OBJECTS=$(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
#set dependency files
DEPS=$(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.d)
# Include all dependency files
-include $(DEPS)
# General targets
all: $(BIN_DIR) $(OBJ_DIR) $(BINARY)
clean:
rm -rf $(OBJ_DIR)
rm -rf $(BIN_DIR)
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
$(BIN_DIR):
mkdir -p $(BIN_DIR)
# Compile and link targets
$(BINARY): $(OBJECTS)
$(CXX) $^ $(LDFLAGS) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) -c $< $(CPPFLAGS) -o $@ -MMD
run: all
$(BINARY)
.PHONY: all clean run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment