1# Makefile for C++ Examples
2# Compile with C++20 standard and common flags
3
4CXX = g++
5CXXFLAGS = -std=c++20 -Wall -Wextra -O2
6LDFLAGS = -pthread
7
8# Source files
9SOURCES = 01_modern_cpp.cpp \
10 02_stl_containers.cpp \
11 03_smart_pointers.cpp \
12 04_threading.cpp \
13 05_design_patterns.cpp \
14 06_templates.cpp \
15 07_move_semantics.cpp
16
17# Object files (replace .cpp with .o)
18OBJECTS = $(SOURCES:.cpp=.o)
19
20# Executables (replace .cpp with no extension)
21EXECUTABLES = $(SOURCES:.cpp=)
22
23# Default target: build all
24.PHONY: all
25all: $(EXECUTABLES)
26
27# Pattern rule: compile .cpp to executable
28%: %.cpp
29 @echo "Compiling $<..."
30 $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
31
32# Individual targets (for convenience)
33.PHONY: modern stl smart threading patterns templates move
34modern: 01_modern_cpp
35stl: 02_stl_containers
36smart: 03_smart_pointers
37threading: 04_threading
38patterns: 05_design_patterns
39templates: 06_templates
40move: 07_move_semantics
41
42# Run all examples
43.PHONY: run
44run: all
45 @echo "\n=== Running all examples ===\n"
46 @for exe in $(EXECUTABLES); do \
47 echo "\n--- Running $$exe ---\n"; \
48 ./$$exe; \
49 echo "\n"; \
50 done
51
52# Run specific example
53.PHONY: run-%
54run-%: %
55 @echo "\n=== Running $< ===\n"
56 ./$<
57
58# Clean build artifacts
59.PHONY: clean
60clean:
61 @echo "Cleaning build artifacts..."
62 rm -f $(EXECUTABLES) $(OBJECTS) *.out core /tmp/raii_test.txt /tmp/test.txt
63
64# Display help
65.PHONY: help
66help:
67 @echo "C++ Examples Makefile"
68 @echo "====================="
69 @echo ""
70 @echo "Targets:"
71 @echo " all - Build all examples (default)"
72 @echo " clean - Remove all build artifacts"
73 @echo " run - Build and run all examples"
74 @echo " run-<name> - Build and run specific example"
75 @echo ""
76 @echo "Individual examples:"
77 @echo " modern - Build 01_modern_cpp"
78 @echo " stl - Build 02_stl_containers"
79 @echo " smart - Build 03_smart_pointers"
80 @echo " threading - Build 04_threading"
81 @echo " patterns - Build 05_design_patterns"
82 @echo " templates - Build 06_templates"
83 @echo " move - Build 07_move_semantics"
84 @echo ""
85 @echo "Examples:"
86 @echo " make # Build all"
87 @echo " make modern # Build modern C++ demo"
88 @echo " make run-threading # Build and run threading demo"
89 @echo " make clean # Clean all build artifacts"
90
91# Compiler and flags info
92.PHONY: info
93info:
94 @echo "Compiler: $(CXX)"
95 @echo "Flags: $(CXXFLAGS)"
96 @echo "Linker flags: $(LDFLAGS)"
97 @$(CXX) --version