Skip to content

Instantly share code, notes, and snippets.

@matteocaberlotto
Created December 11, 2023 12:25
Show Gist options
  • Save matteocaberlotto/c80c449365576e05dffd4225d2923616 to your computer and use it in GitHub Desktop.
Save matteocaberlotto/c80c449365576e05dffd4225d2923616 to your computer and use it in GitHub Desktop.
A nice Makefile for C++ projects
CXX := -c++
CXXFLAGS := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS := -L/usr/lib -lstdc++ -lm
BUILD := ./build
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/apps
TARGET := thegame
INCLUDE := -Iinclude/
SRC := \
$(wildcard src/player/*.cpp) \
$(wildcard src/score/*.cpp) \
$(wildcard src/playground/*.cpp) \
$(wildcard *.cpp) \
OBJECTS := $(SRC:%.cpp=$(OBJ_DIR)/%.o)
DEPENDENCIES \
:= $(OBJECTS:.o=.d)
all: build $(APP_DIR)/$(TARGET)
$(OBJ_DIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -MMD -o $@
$(APP_DIR)/$(TARGET): $(OBJECTS)
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)
-include $(DEPENDENCIES)
.PHONY: all build clean debug release info
build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)
debug: CXXFLAGS += -DDEBUG -g
debug: all
release: CXXFLAGS += -O2
release: all
clean:
-@rm -rvf $(OBJ_DIR)/*
-@rm -rvf $(APP_DIR)/*
info:
@echo "[*] Application dir: ${APP_DIR} "
@echo "[*] Object dir: ${OBJ_DIR} "
@echo "[*] Sources: ${SRC} "
@echo "[*] Objects: ${OBJECTS} "
@echo "[*] Dependencies: ${DEPENDENCIES}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment