-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
33 lines (25 loc) · 962 Bytes
/
makefile
File metadata and controls
33 lines (25 loc) · 962 Bytes
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
CXX = g++
CXXFLAGS = -std=c++11 -Wall -Wextra
LDFLAGS = -lm
SRC_DIR = src
BUILD_DIR = build
TARGET = $(BUILD_DIR)/atm
# 只匹配 cpp 源文件(不要把头文件也加进来)
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
# 将 src/xxx.cpp 映射为 build/xxx.o
OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
# 说明:在生成对象文件规则中确保创建目标目录,避免每次 make 都执行 mkdir
.PHONY: all clean
all: $(TARGET)
@echo "Compilation complete! Executable file: $(TARGET)" # 编译完成提示
@echo "Run the program using: $(TARGET)" # 运行提示
# 链接可执行文件
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) $(OBJS) -o $@ $(LDFLAGS)
# 生成对象文件时,先确保目标目录存在(支持子目录)
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
-rm -rf $(BUILD_DIR) # 清理构建目录
@echo "Cleaned all build files" # 清理完成提示