-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
58 lines (45 loc) · 1.53 KB
/
Copy pathmakefile
File metadata and controls
58 lines (45 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Makefile for C++ OOP Project (Optimized & Recursive)
# Compiler settings
CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++17 -I include
# Directories
SRC_DIR := src
OBJ_DIR := build
BIN_DIR := bin
INCLUDE_DIR := include
DATA_DIR := data
CONFIG_DIR := config
# Target executable
TARGET := $(BIN_DIR)/game
# 1. Recursive Source Finding
# Secara otomatis mencari semua file .cpp di dalam src/ dan semua sub-foldernya
SRCS := $(shell find $(SRC_DIR) -name '*.cpp')
# 2. Dynamic Object Mapping
# Mengubah path src/xxx/yyy.cpp menjadi build/xxx/yyy.o
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
# Main targets
all: directories $(TARGET)
# Create necessary root directories
directories:
@if [ ! -d "$(OBJ_DIR)" ]; then mkdir "$(OBJ_DIR)"; fi
@if [ ! -d "$(BIN_DIR)" ]; then mkdir "$(BIN_DIR)"; fi
@if [ ! -d "$(DATA_DIR)" ]; then mkdir "$(DATA_DIR)"; fi
@if [ ! -d "$(CONFIG_DIR)" ]; then mkdir "$(CONFIG_DIR)"; fi
# Link object files to create executable
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
@echo "Build successful! Executable is at $(TARGET)"
# Compile source files into object files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
@if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
$(CXX) $(CXXFLAGS) -c $< -o $@
# Run the game
run: all
./$(TARGET)
# Clean up generated files
clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)
@echo "Cleaned up $(OBJ_DIR) and $(BIN_DIR)"
# Rebuild everything from scratch
rebuild: clean all
.PHONY: all clean rebuild run directories