1+ # ==============================================================================
2+ # Basic C++ Makefile Template
3+ # Author: Dayron Mustelier (@DMsuDev) - 2025/2026
4+ # License: MIT
5+ # ==============================================================================
6+
7+ # Default build type when running plain `make`
8+ .DEFAULT_GOAL := all
9+
10+ # ──────────────────────────────────────────────────────────────────────────────
11+ # Main configuration – customize these variables
12+ # ──────────────────────────────────────────────────────────────────────────────
13+
14+ APP_NAME ?= ProjectName
15+ SRC_EXT ?= cpp
16+ LANGUAGE ?= c++23
17+ CXX := g++
18+
19+ # ──────────────────────────────────────────────────────────────────────────────
20+ # Directories & Paths
21+ # ──────────────────────────────────────────────────────────────────────────────
22+
23+ # Can be a list separate by spaces " ": include/ src/ src/core imgui/
24+ SOURCE_DIRS ?= src
25+ INCLUDE_DIRS ?= include
26+
27+ BUILD_BASE ?= build
28+ BIN_DIR ?= $(BUILD_BASE ) /bin
29+ OBJ_DIR ?= $(BUILD_BASE ) /obj
30+ DEP_DIR ?= $(BUILD_BASE ) /dep
31+
32+ # ──────────────────────────────────────────────────────────────────────────────
33+ # Compiler & Linker flags (base)
34+ # ──────────────────────────────────────────────────────────────────────────────
35+
36+ CXXFLAGS_BASE := -std=$(LANGUAGE ) -fdiagnostics-color=always
37+
38+ # Set flags that control and shows the error of the compiler
39+ # If type -Werror the compiler when throw error message stop the compilation.
40+ ERRORFLAGS := -Wall -Wextra -pedantic-errors
41+
42+ CXXFLAGS := $(CXXFLAGS_BASE ) $(ERRORFLAGS )
43+ LDFLAGS :=
44+
45+ # Show console window on Windows? (set to false for GUI-only apps) (Windows)
46+ USE_CONSOLE ?= true
47+ ifeq ($(USE_CONSOLE ) ,false)
48+ LDFLAGS += -mwindows
49+ # LDFLAGS += -luser32 -lgdi32
50+ endif
51+
52+ # ──────────────────────────────────────────────────────────────────────────────
53+ # Automatic source & object discovery
54+ # ──────────────────────────────────────────────────────────────────────────────
55+
56+ # Find all source files recursively
57+ define recurse
58+ $(wildcard $(1 ) /* .$(SRC_EXT ) ) \
59+ $(foreach d,$(wildcard $(1 ) /* ) , \
60+ $(if $(wildcard $(d ) /) ,$(call recurse,$(d ) ) ) )
61+ endef
62+
63+ SOURCES := $(foreach dir,$(SOURCE_DIRS ) ,$(call recurse,$(dir ) ) )
64+ OBJECTS := $(patsubst % .$(SRC_EXT ) ,$(OBJ_DIR ) /% .o,$(SOURCES ) )
65+ DEPENDENCIES := $(OBJECTS:.o=.d )
66+
67+ INCLUDES := $(addprefix -I,$(INCLUDE_DIRS ) )
68+ LIB_DIRS ?= -L./lib
69+ LIBS ?=
70+
71+ LDFLAGS += $(LIB_DIRS ) $(addprefix -l,$(LIBS ) )
72+
73+ # Generate files that encode make rules for the .h dependencies
74+ DEPFLAGS = -MT $@ -MMD -MP -MF $(DEP_DIR ) /$* .d
75+
76+ # ──────────────────────────────────────────────────────────────────────────────
77+ # Build rules
78+ # ──────────────────────────────────────────────────────────────────────────────
79+
80+ .PHONY : all dirs
81+
82+ all : clean-banner dirs $(BIN_DIR ) /$(APP_NAME )
83+
84+ dirs :
85+ @mkdir -p $(BIN_DIR ) $(OBJ_DIR ) $(DEP_DIR )
86+
87+ $(OBJ_DIR ) /% .o : % .$(SRC_EXT ) | dirs
88+ @printf " [Compiling] -> %s\n" " $<"
89+ @mkdir -p $(@D ) $(dir $(DEP_DIR ) /$* .d) 2> /dev/null || true
90+ @$(CXX ) $(CXXFLAGS ) $(INCLUDES ) $(DEPFLAGS ) -c $< -o $@ 2> compile.log || \
91+ { printf " \n[ERROR] Compilation failed for $<\n" ; cat compile.log; rm -f compile.log; exit 1; }
92+ @rm -f compile.log
93+
94+ $(BIN_DIR ) /$(APP_NAME ) : $(OBJECTS )
95+ @printf " \n─── Linking → %s\n" " $@ "
96+ @$(CXX ) $^ -o $@ $(LDFLAGS ) && \
97+ printf " %-12s: Linked successfully\n" " Status" || \
98+ { printf " %-12s: Linking failed\n" " Status" ; exit 1; }
99+
100+ $(DEP_DIR ) /% .d : ;
101+ .PRECIOUS : $(DEP_DIR ) /% .d
102+
103+ -include $(DEPENDENCIES )
104+
105+ # ──────────────────────────────────────────────────────────────────────────────
106+ # Build variants
107+ # ──────────────────────────────────────────────────────────────────────────────
108+
109+ .PHONY : debug release
110+ debug : CXXFLAGS += -Og -g -ggdb3 -DDEBUG
111+ debug : all
112+
113+ release : CXXFLAGS += -O3 -DNDEBUG -march=native
114+ release : all
115+
116+ # ──────────────────────────────────────────────────────────────────────────────
117+ # Utility targets
118+ # ──────────────────────────────────────────────────────────────────────────────
119+
120+ .PHONY : clean-banner run clean help
121+
122+ clean-banner :
123+ @clear 2> /dev/null || cls
124+
125+ run : release
126+ @$(BIN_DIR ) /$(APP_NAME )
127+
128+ clean :
129+ @rm -rf $(BUILD_BASE )
130+ @printf " Cleaned build directory\n"
131+
132+ help :
133+ @echo " Available targets:"
134+ @echo " make [all] → Build (default)"
135+ @echo " make debug → Build with debug symbols"
136+ @echo " make release → Build optimized"
137+ @echo " make run → Build release + execute"
138+ @echo " make clean → Remove build artifacts"
139+ @echo " make help → Show this help"
0 commit comments