-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
executable file
·71 lines (60 loc) · 2.51 KB
/
CMakeLists.txt
File metadata and controls
executable file
·71 lines (60 loc) · 2.51 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
59
60
61
62
63
64
65
66
67
68
69
70
71
cmake_minimum_required(VERSION 3.10)
project(WebServer)
# ====================================================
# 1. 基础设置
# ====================================================
# 统一使用 C++17 (兼容性更好,涵盖 C++14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ====================================================
# 2. 依赖查找与引入
# ====================================================
# [Spdlog] 日志库 (源码编译方式)
# 确保 third_party/spdlog 目录存在
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog")
add_subdirectory(third_party/spdlog)
else()
message(WARNING "spdlog source not found in third_party!")
endif()
# [MySQL] 数据库依赖
find_package(PkgConfig REQUIRED)
pkg_check_modules(MYSQL REQUIRED mysqlclient)
# [Threads] 线程库
find_package(Threads REQUIRED)
# ====================================================
# 3. 源文件与生成目标
# ====================================================
# 自动递归查找 src 下所有的 .cpp 文件
# (包含 main.cpp, net/, server/, base/, http/, app/ 等所有子目录)
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
# 生成可执行文件 (仅定义一次)
add_executable(WebServer ${SRC_FILES})
# ====================================================
# 4. 头文件包含路径
# ====================================================
target_include_directories(WebServer PUBLIC
${CMAKE_SOURCE_DIR}/include # 项目头文件
${CMAKE_SOURCE_DIR}/third_party # 第三方库 (如 nlohmann/json)
${MYSQL_INCLUDE_DIRS} # MySQL 头文件
)
# ====================================================
# 5. 链接库
# ====================================================
target_link_libraries(WebServer
PRIVATE
spdlog::spdlog # spdlog 库
Threads::Threads # 线程库
${MYSQL_LIBRARIES} # MySQL 客户端库
dl # 动态链接库 (MySQL依赖)
)
# ====================================================
# 6. 构建后操作 (资源复制)
# ====================================================
# 编译成功后,自动将 resources 文件夹复制到 build 目录
# 这样程序运行时可以直接读取 resources/config.json
add_custom_command(TARGET WebServer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/resources
${CMAKE_CURRENT_BINARY_DIR}/resources
COMMENT "Copying resources to build directory..."
)