CMake TcpServer项目 生成静态库.a / 动态库.so

CMake 实战构建TcpServer项目 静态库/动态库-CSDN博客icon-default.png?t=N7T8https://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.cpp15 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}) # 库生成的路径 LIBPATHadd_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}) # 库生成的路径 LIBPATHadd_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}) # 库生成的路径 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/ 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/ 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}/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}) # 库生成的路径 LIBPATHadd_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}) # 库生成的路径 LIBPATHadd_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}) # 库生成的路径 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/ 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/ 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}/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 变量

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/415438.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IPFoxy运营干货|谷歌广告Google Ads建立广告需要注意什么?

投放谷歌广告需要多少个步骤和什么准备工作,本文将来讲述,主要分5个内容:一、投放前竞对研究;二、投放前广告账户设置;三、建立广告系列;四、建立广告组;五、广告长期策略。接下来我们来开始建立…

AI视觉训练环境-【2】开发客户端搭建

本文介绍搭建一套简单的AI视觉训练环境的过程,作者也是刚开始接触机器视觉,现将学习过程中的搭建过程梳理下来分享给大家。创作过程实属不易,如不喜欢请轻轻绕过,如有建议无比欢迎,只愿我们共同进步。 训练环境 1.概述 开发环境基于yolov5进行训练演练,开发人员通过PyCha…

Gazebo的模型下载。

git clone zouxu634866/gazebo_modelshttps://gitee.com/zouxu6348660/gazebo_models.git,并完成路径配置。 (本文提供了gitee下载,国外的Github下载较慢。)

16位单片机单片机S1C17153

16位单片机单片机 .16KB ROM / 2KB内存 * S1C17653对于程序开发很有用。 .产生具有内置振荡器的操作时钟。 - OSC3B振荡器电路:2MHz/1MHz/500kHz(类型)内部振荡器电路 -OSC1无振荡器电路:32.768 kHz(类型&#xf…

【C语言】linux内核ipoib模块 - ipoib_intf_init

一、ipoib_intf_init函数定义 int ipoib_intf_init(struct ib_device *hca, u8 port, const char *name,struct net_device *dev, struct ipoib_dev_priv *priv) {struct rdma_netdev *rn netdev_priv(dev);int rc;priv->ca hca;priv->port port;rc rdma_init_netde…

微信小程序(四)页面跳转

注释很详细&#xff0c;直接上代码 上一篇 新增内容 1.相对路径页面跳转 2. 绝对路径页面跳转 index.wxml <!-- navigator是块级元素&#xff0c;占一整行 --> <!-- 页面跳转url&#xff0c;相对路径 --> <navigator url"../logs/logs"><butto…

匿名/箭头函数,立即执行函数IIFE;函数声明式和函数表达式

目录 匿名/箭头函数&#xff1a;简洁 继承上一层作用域链的this 不绑定arguments,用rest参数 rest 参数&#xff1a;...真正的数组 因为没有function声明&#xff0c;所以没有原型prototype&#xff0c;所以不能作为构造函数 当函数体只有一句时&#xff0c;可省 return ,…

c++:基于c语言基础上的语法不同(1)

前言&#xff1a;此篇文章适合学完c语言基础概念的同学&#xff0c;是帮助c向c语言的同学快速掌握基本语法。 基础格式 #include<iostream>using namespace std; int main() {system("pause");return 0; } 输入&#xff1a; cin>>a;//a是输入内容 输出…

MySQL面试总结

MySQL优化 1.MySQL如何定位慢查询 1.1开源工具 1.2MySQL自带慢日志 1.3解答 2.EXPLAIN 2.1解答 3.什么是索引 4.B树 4.1数据结构对比 5.聚簇索引&#xff08;聚集索引&#xff09; 6.覆盖索引 7.索引创建原则 8.什么情况下索引失效 9.你对sql优化经验 10.事务 11.MVCC 11.主从…

力扣 | 438. 找到字符串中所有字母异位词

滑动窗口解题 示例 在s里面控制一个p字符串长度的滑动窗口&#xff0c;统计该滑动窗口中的每种字符出现的次数 import java.util.ArrayList; import java.util.Arrays; import java.util.List;public class Problem_438_FindAnagrams {public List<Integer> findAnagram…

「JavaSE」类和对象3

&#x1f387;个人主页&#xff1a;Ice_Sugar_7 &#x1f387;所属专栏&#xff1a;快来卷Java啦 &#x1f387;欢迎点赞收藏加关注哦&#xff01; 类和对象3 &#x1f349;多态&#x1f34c;重写&#x1f34c;向上转型&向下转型&#x1f34c;静态绑定&动态绑定&#x…

FFmpeg之SWScale

文章目录 一、概述二、函数调用结构图三、Libswscale处理数据流程四、重要结构体4.1、SwsContext4.2、SwsFilter 五、重要函数5.1、sws_getContext5.1.1、sws_alloc_context5.1.2、sws_init_context 5.2、sws_scale5.2.1、SwsContext中的swscale()5.2.2、check_image_pointers5…