CMake 实战构建TcpServer项目 静态库/动态库-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/135608829?spm=1001.2014.3001.5501 在这篇博客的基础上,我们把头文件放在include里边,把源文件放在src里边,重新构建
heheda@linux:~/Linux/LinuxServerCppInclude$ tree
.
├── CMakeLists.txt
├── common
│ ├── CMakeLists.txt
│ ├── include
│ │ ├── Buffer.h
│ │ ├── Channel.h
│ │ └── Log.h
│ └── src
│ ├── Buffer.cpp
│ └── Channel.cpp
├── http
│ ├── CMakeLists.txt
│ ├── include
│ │ ├── HttpRequest.h
│ │ └── HttpResponse.h
│ └── src
│ ├── HttpRequest.cpp
│ └── HttpResponse.cpp
├── main.cpp
├── reactor
│ ├── CMakeLists.txt
│ ├── include
│ │ ├── Dispatcher.h
│ │ ├── EpollDispatcher.h
│ │ ├── EventLoop.h
│ │ ├── PollDispatcher.h
│ │ └── SelectDispatcher.h
│ └── src
│ ├── Dispatcher.cpp
│ ├── EpollDispatcher.cpp
│ ├── EventLoop.cpp
│ ├── PollDispatcher.cpp
│ └── SelectDispatcher.cpp
├── tcp
│ ├── CMakeLists.txt
│ ├── include
│ │ ├── TcpConnection.h
│ │ └── TcpServer.h
│ └── src
│ ├── TcpConnection.cpp
│ └── TcpServer.cpp
└── thread
├── CMakeLists.txt
├── include
│ ├── ThreadPool.h
│ └── WorkerThread.h
└── src
├── ThreadPool.cpp
└── WorkerThread.cpp
15 directories, 34 files
heheda@linux:~/Linux/LinuxServerCppInclude$
一、静态库
1.在common文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(common)
# 搜索源文件
aux_source_directory(./src/ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(common STATIC ${SRC}) # 静态库
2.在http文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(http)
# 搜索源文件
aux_source_directory(./src/ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(http STATIC ${SRC}) # 静态库
3.在reactor文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(reactor)
aux_source_directory(./src/ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_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/ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(tcp STATIC ${SRC}) # 静态库
5.在thread文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(thread)
aux_source_directory(./src/ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(thread STATIC ${SRC}) # 静态库
6.在根目录中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HttpWeb)
# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/staticLib)
include_directories(${PROJECT_SOURCE_DIR}/common/include)
include_directories(${PROJECT_SOURCE_DIR}/http/include)
include_directories(${PROJECT_SOURCE_DIR}/reactor/include)
include_directories(${PROJECT_SOURCE_DIR}/tcp/include)
include_directories(${PROJECT_SOURCE_DIR}/thread/include)
# 给当前节点添加子目录
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 变量
heheda@linux:~/Linux/LinuxServerCppInclude$ mkdir build
heheda@linux:~/Linux/LinuxServerCppInclude$ cd build/
heheda@linux:~/Linux/LinuxServerCppInclude/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/heheda/Linux/LinuxServerCppInclude/build
heheda@linux:~/Linux/LinuxServerCppInclude/build$ make
Scanning dependencies of target thread
[ 5%] Building CXX object thread/CMakeFiles/thread.dir/src/ThreadPool.cpp.o
[ 10%] Building CXX object thread/CMakeFiles/thread.dir/src/WorkerThread.cpp.o
[ 15%] Linking CXX static library ../../staticLib/thread/libthread.a
[ 15%] Built target thread
Scanning dependencies of target common
[ 20%] Building CXX object common/CMakeFiles/common.dir/src/Buffer.cpp.o
[ 25%] Building CXX object common/CMakeFiles/common.dir/src/Channel.cpp.o
[ 30%] Linking CXX static library ../../staticLib/common/libcommon.a
[ 30%] Built target common
Scanning dependencies of target http
[ 35%] Building CXX object http/CMakeFiles/http.dir/src/HttpRequest.cpp.o
[ 40%] Building CXX object http/CMakeFiles/http.dir/src/HttpResponse.cpp.o
[ 45%] Linking CXX static library ../../staticLib/http/libhttp.a
[ 45%] Built target http
Scanning dependencies of target reactor
[ 50%] Building CXX object reactor/CMakeFiles/reactor.dir/src/Dispatcher.cpp.o
[ 55%] Building CXX object reactor/CMakeFiles/reactor.dir/src/EpollDispatcher.cpp.o
[ 60%] Building CXX object reactor/CMakeFiles/reactor.dir/src/EventLoop.cpp.o
[ 65%] Building CXX object reactor/CMakeFiles/reactor.dir/src/PollDispatcher.cpp.o
[ 70%] Building CXX object reactor/CMakeFiles/reactor.dir/src/SelectDispatcher.cpp.o
[ 75%] Linking CXX static library ../../staticLib/reactor/libreactor.a
[ 75%] Built target reactor
Scanning dependencies of target tcp
[ 80%] Building CXX object tcp/CMakeFiles/tcp.dir/src/TcpConnection.cpp.o
[ 85%] Building CXX object tcp/CMakeFiles/tcp.dir/src/TcpServer.cpp.o
[ 90%] Linking CXX static library ../../staticLib/tcp/libtcp.a
[ 90%] Built target tcp
Scanning dependencies of target server
[ 95%] Building CXX object CMakeFiles/server.dir/main.cpp.o
[100%] Linking CXX executable ../bin/server
[100%] Built target server
heheda@linux:~/Linux/LinuxServerCppInclude/build$ cd ../bin
heheda@linux:~/Linux/LinuxServerCppInclude/bin$ ./server
DEBUG: /home/heheda/Linux/LinuxServerCppInclude/tcp/src/TcpServer.cpp@run, line: 66
***LogInfo[服务器程序已经启动了...]
二、动态库
1.在common文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(common)
# 搜索源文件
aux_source_directory(./src/ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(common SHARED ${SRC}) # 动态库
2.在http文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(http)
# 搜索源文件
aux_source_directory(./src/ SRC)
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(http SHARED ${SRC}) # 动态库
3.在reactor文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(reactor)
aux_source_directory(./src/ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_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/ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(tcp SHARED ${SRC}) # 动态库
5.在thread文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(thread)
aux_source_directory(./src/ SRC)# 搜索源文件
set(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(thread SHARED ${SRC}) # 动态库
6.在根目录中的CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HttpWeb)
# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/sharedLib)
include_directories(${PROJECT_SOURCE_DIR}/common/include)
include_directories(${PROJECT_SOURCE_DIR}/http/include)
include_directories(${PROJECT_SOURCE_DIR}/reactor/include)
include_directories(${PROJECT_SOURCE_DIR}/tcp/include)
include_directories(${PROJECT_SOURCE_DIR}/thread/include)
# 给当前节点添加子目录
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)
target_link_libraries(server common http reactor tcp thread -lpthread)
# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量