CMake 实战构建TcpServer项目 静态库/动态库

爱编程的大丙CMake: 

20. 举例 - 下_哔哩哔哩_bilibiliicon-default.png?t=N7T8https://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 变量

三、一键运行项目

  • launch.json
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${workspaceFolder}/bin/server","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": true,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},],"preLaunchTask": "Build",// "miDebuggerPath": "/usr/bin/gdb"},]
}
  • tasks.json
{"version": "2.0.0","options": {"cwd": "${workspaceFolder}/build"},"tasks": [{"type": "shell","label": "cmake","command": "cmake","args": [".."]},{"label": "make","group": "build","command": "make","args": [],"problemMatcher": []},{"label": "Build","dependsOrder": "sequence","dependsOn": ["cmake","make"]},{"type": "cppbuild","label": "C/C++: g++ 生成活动文件","command": "/usr/bin/g++","args": ["-fdiagnostics-color=always","-g", // 生成和调试有关的信息"${file}","-lpthread","-o","${workspaceFolder}/bin/server",],"options": {"cwd": "${workspaceFolder}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "编译器: /usr/bin/g++"}]
}

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

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

相关文章

经典计算机网络面试题

1.说说HTTP常用的状态码及其含义? 状态码 类别 1xx 信息性状态码 2xx 成功状态码 3xx 重定向状态码 4xx 客户端错误状态码 5xx 服务端错误状态码 日常开发中的状态码: 状态码 含义 101 切换请求协议 200 请求成功 301 永久性重定向&…

UE5 C++ 基础变量类型,关于框架的初级练习

一.创建自己的MyGameModed。并在其中设置好GamePlay框架。 1.创建MyGameState,MyGameState,MyHUD,MyPawn,MyPawn,MyPlayerController,MyPlayerState。 2.并在MyGameMode的头文件里面,把GmaeMode里的框架需要的框架类都包含进去。 3.写一个构…

Spring 核心之 IOC 容器学习一

IOC 与 DI IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创建、依赖的代码,反转给容器来帮忙实现。那么必然的我们需要创建一个容器,同时需要一种描述来让容器知道需要创建的对象与对象…

电力能源三维可视化合集 | 图扑数字孪生

电力能源是现代社会发展和运行的基石,渗透于工业、商业、农业、家庭生活等方方面面,它为经济、生活质量、环境保护和社会发展提供了巨大的机会和潜力。图扑软件应用自研 HT for Web 强大的渲染引擎,助力现代化的电力能源数字孪生场景&#xf…

微信小程序(六)tabBar的使用

注释很详细,直接上代码 上一篇 新增内容: 1. 标签栏文字的内容以及默认与选中颜色 2. 标签栏图标的默认样式与选中样式 3. 标签选项路径页面 4.标签栏背景颜色 🐼(文末补充)设置标签栏后为什么navigator标签无法跳转页…

最新版git2.43安装、记住用户名和密码以及tortoisegit2.15使用

一、下载git 打开git官网地址:https://git-scm.com/进行下载 下载完安装,一直next就好,如果愿意就可以改下安装路径,改在d盘。 具体可以参考:git安装教程 二、安装完下载小乌龟以及中文语言包 下载地址:…

253:vue+openlayers 加载HERE多种地图(v2软件版本)

第253个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+openlayers中添加HERE地图,并且含多种的表现形式。包括地图类型,文字标记的设置、语言的选择、PPI的设定。 直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果 文章目录 示例效果图配置方式示例源…

【STM32】| 02——常用外设 | I2C

系列文章目录 【STM32】| 01——常用外设 | USART 【STM32】| 02——常用外设 | I2C 失败了也挺可爱,成功了就超帅。 文章目录 前言1. 简介2. I2C协议2.1 I2C物理连接2.2 I2C通信协议2.2.1 起始和停止信号2.2.2 数据有效性2.2.3 数据传输格式2.2.4 从机地址/数据方…

腾讯云轻量化应用服务器_轻量化应用服务器_轻量化私有云

腾讯云轻量应用服务器开箱即用、运维简单的轻量级云服务器,CPU内存带宽配置高并且价格特别便宜,大带宽,但是限制月流量,轻量2核2G3M带宽62元一年、2核2G4M优惠价118元一年,540元三年、2核4G5M带宽218元一年&#xff0c…

esp32-idf eclipse 定时器的使用demo

esp32定时器的使用demo 1、介绍 ESP32芯片包含两个硬件定时器组。每组有两个通用硬件定时器。它们都是基于16位预分频器和64位自动重载功能的向上向下计数器的64位通用定时器。 2、API接口函数 创建定时器函数: esp_timer_create(); esp_err_t esp_timer_create …

阿里云ECS使用docker搭建mysql服务

目录 1.确保正确安装好docker 2.安装mysql镜像 3.创建容器(设置端口映射、目录映射) 1.确保正确安装好docker 安装教程: 阿里云ECS(CentOS镜像)安装docker-CSDN博客https://blog.csdn.net/qq_62262918/article/details/135686614?spm10…

DAY12--learning English

一、积累 1.superstition 2.intersection If you are in an intersection and heard siren of an emergency vehicle. you should. 当你身处于道路交界处并且听到应急车辆的鸣笛声,你应该. 3. luxurious Japans $3000 Most luxurious sleeper train. 花费3000美元…