爱编程的大丙CMake:
20. 举例 - 下_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV14s4y1g7Zj?p=20&spm_id_from=pageDriver&vd_source=a934d7fc6f47698a29dac90a922ba5a3
heheda@linux:~/Linux/LinuxServerCpp$ ls
Buffer.cpp Log.h
Buffer.h main.cpp
Channel.cpp PollDispatcher.cpp
Channel.h PollDispatcher.h
Dispatcher.cpp SelectDispatcher.cpp
Dispatcher.h SelectDispatcher.h
EpollDispatcher.cpp TcpConnection.cpp
EpollDispatcher.h TcpConnection.h
EventLoop.cpp TcpServer.cpp
EventLoop.h TcpServer.h
HttpRequest.cpp ThreadPool.cpp
HttpRequest.h ThreadPool.h
HttpResponse.cpp WorkerThread.cpp
HttpResponse.h WorkerThread.h
heheda@linux:~/Linux/LinuxServerCpp$ mkdir http
heheda@linux:~/Linux/LinuxServerCpp$ mv Http* http
heheda@linux:~/Linux/LinuxServerCpp$ tree http/
http/
├── HttpRequest.cpp
├── HttpRequest.h
├── HttpResponse.cpp
└── HttpResponse.h0 directories, 4 files
heheda@linux:~/Linux/LinuxServerCpp$ mkdir tcp
heheda@linux:~/Linux/LinuxServerCpp$ mv Tcp* tcp
heheda@linux:~/Linux/LinuxServerCpp$ tree tcp/
tcp/
├── TcpConnection.cpp
├── TcpConnection.h
├── TcpServer.cpp
└── TcpServer.h0 directories, 4 files
heheda@linux:~/Linux/LinuxServerCpp$ mkdir reactor
heheda@linux:~/Linux/LinuxServerCpp$ mv *Dispatcher* reactor/
heheda@linux:~/Linux/LinuxServerCpp$ tree reactor/
reactor/
├── Dispatcher.cpp
├── Dispatcher.h
├── EpollDispatcher.cpp
├── EpollDispatcher.h
├── PollDispatcher.cpp
├── PollDispatcher.h
├── SelectDispatcher.cpp
└── SelectDispatcher.h0 directories, 8 files
heheda@linux:~/Linux/LinuxServerCpp$ mkdir thread
heheda@linux:~/Linux/LinuxServerCpp$ mv *Thread* thread/
heheda@linux:~/Linux/LinuxServerCpp$ tree thread/
thread/
├── ThreadPool.cpp
├── ThreadPool.h
├── WorkerThread.cpp
└── WorkerThread.h0 directories, 4 files
heheda@linux:~/Linux/LinuxServerCpp$ mkdir common
heheda@linux:~/Linux/LinuxServerCpp$ mv Buffer.* Channel.* Log.h common
heheda@linux:~/Linux/LinuxServerCpp$ tree common/
common/
├── Buffer.cpp
├── Buffer.h
├── Channel.cpp
├── Channel.h
└── Log.h0 directories, 5 files
heheda@linux:~/Linux/LinuxServerCpp$ mv EventLoop.* reactor/
heheda@linux:~/Linux/LinuxServerCpp$ ls
common http main.cpp reactor tcp thread
heheda@linux:~/Linux/LinuxServerCpp$
一、静态库
1.在common文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(common)# 搜索源文件
aux_source_directory(./ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(common STATIC ${SRC}) # 静态库
2.在http文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(http)# 搜索源文件
aux_source_directory(./ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(http STATIC ${SRC}) # 静态库
3.在reactor文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(reactor)
aux_source_directory(./ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(reactor STATIC ${SRC}) # 静态库
4.在tcp文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(tcp)link_libraries(common http reactor)
aux_source_directory(./ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(tcp STATIC ${SRC}) # 静态库
5.在thread文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(thread)
aux_source_directory(./ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(thread STATIC ${SRC}) # 静态库
6.在根目录中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HttpWeb)# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/lib)include_directories(${PROJECT_SOURCE_DIR}/common)
include_directories(${PROJECT_SOURCE_DIR}/http)
include_directories(${PROJECT_SOURCE_DIR}/reactor)
include_directories(${PROJECT_SOURCE_DIR}/tcp)
include_directories(${PROJECT_SOURCE_DIR}/thread)# 给当前节点添加子目录
add_subdirectory(common)
add_subdirectory(http)
add_subdirectory(reactor)
add_subdirectory(tcp)
add_subdirectory(thread)link_libraries(common http reactor tcp thread -lpthread)
add_executable(server main.cpp)# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量
- 注意:cmake 编译c++文件时, 对‘pthread_create’未定义的引用等错误
1.或者2.这种写法
1.target_link_libraries(common http reactor tcp thread-lpthread) 2.link_libraries(common http reactor tcp thread -lpthread)
二、动态库
1.在common文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(common)# 搜索源文件
aux_source_directory(./ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(common SHARED ${SRC}) # 动态库
2.在http文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(http)# 搜索源文件
aux_source_directory(./ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(http SHARED ${SRC}) # 动态库
3.在reactor文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(reactor)
aux_source_directory(./ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(reactor SHARED ${SRC}) # 动态库
4.在tcp文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(tcp)link_libraries(common http reactor)
aux_source_directory(./ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(tcp SHARED ${SRC}) # 动态库
5.在thread文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(thread)
aux_source_directory(./ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATHadd_library(thread SHARED ${SRC}) # 动态库
6.在根目录中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HttpWeb)# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/lib)include_directories(${PROJECT_SOURCE_DIR}/common)
include_directories(${PROJECT_SOURCE_DIR}/http)
include_directories(${PROJECT_SOURCE_DIR}/reactor)
include_directories(${PROJECT_SOURCE_DIR}/tcp)
include_directories(${PROJECT_SOURCE_DIR}/thread)# 给当前节点添加子目录
add_subdirectory(common)
add_subdirectory(http)
add_subdirectory(reactor)
add_subdirectory(tcp)
add_subdirectory(thread)add_executable(server main.cpp)
target_link_libraries(server common http reactor tcp thread -lpthread)# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量