-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
52 lines (40 loc) · 1.13 KB
/
Makefile
File metadata and controls
52 lines (40 loc) · 1.13 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
# Lesson 15: Testing
#
# The `test` target compiles and runs the test executable.
# If any test fails (non-zero exit), Make reports an error.
#
# Pattern:
# make → build the app
# make test → build and run tests
# make all → build both app and tests (but don't run)
#
# The test binary links against the same library code as
# the app (here it's header-only, so just includes).
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Iinclude -MMD -MP
BUILDDIR = build
APP_TARGET = $(BUILDDIR)/app
TEST_TARGET = $(BUILDDIR)/test_stack
APP_SRCS = src/main.cpp
TEST_SRCS = tests/test_stack.cpp
APP_OBJS = $(APP_SRCS:%.cpp=$(BUILDDIR)/%.o)
TEST_OBJS = $(TEST_SRCS:%.cpp=$(BUILDDIR)/%.o)
ALL_DEPS = $(APP_OBJS:.o=.d) $(TEST_OBJS:.o=.d)
.PHONY: all clean test
all: $(APP_TARGET) $(TEST_TARGET)
$(APP_TARGET): $(APP_OBJS)
@mkdir -p $(@D)
$(CXX) -o $@ $^
$(TEST_TARGET): $(TEST_OBJS)
@mkdir -p $(@D)
$(CXX) -o $@ $^
# Build and run tests
test: $(TEST_TARGET)
@echo "Running tests..."
@./$(TEST_TARGET)
$(BUILDDIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
rm -rf $(BUILDDIR)
-include $(ALL_DEPS)