# cpp-server-scaffolding **Repository Path**: czyt1988/cpp-server-scaffolding ## Basic Information - **Project Name**: cpp-server-scaffolding - **Description**: 快速生成一个标准的服务器开发模板,通过cmake一键编译集成,集成了网络基础框架(asio(非boost版本)),日志库(spdlog),json库(nlohmann-json,rapidjson),xml库(tinyxml2),ini配置读取(mINI),中间件:redis库(hiredis,redis++)、kafka库(RdKafka) - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 3 - **Created**: 2024-09-08 - **Last Updated**: 2025-08-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 服务器开发脚手架 快速生成一个标准的服务器开发模板,同时拉取第三方库 cpp开发服务项目的脚手架,从零开始快捷构建一个能快速开发的服务器框架环境,主要适用于大规模并发的底层服务器开发,通过cmake一键编译集成第三方库,避免开发环境安装耗时耗力 集成了网络基础框架(asio(非boost版本)),日志库(spdlog),json库(nlohmann-json,rapidjson),xml库(tinyxml2),ini配置读取(mINI),中间件:redis库(hiredis,redis++)、kafka库(RdKafka) # 如何使用 ## 项目拉取 > 此项目不支持下载为zip包,因为项目的第三方库是用git的submodule管理 首先是git的拉取: 如果你依赖所有的第三方库,那么你直接运行下面这句话即可 ```shell git clone --recurse-submodules https://gitee.com/czyt1988/cpp-server-scaffolding.git ``` 或者: ```shell git clone https://gitee.com/czyt1988/cpp-server-scaffolding.git、 cd cpp-server-scaffolding git submodule update --init --recursive ``` 如果你不是所有第三方都需要,你可以先克隆项目,submodule再单独拉取 ```shell git clone https://gitee.com/czyt1988/cpp-server-scaffolding.git cd cpp-server-scaffolding git submodule update ./3rdparty/{3rdparty-name} ``` 其中`{3rdparty-name}`可以是以下值: - asio - spdlog - nlohmann-json - rapidjson - tinyxml2 - mINI - hiredis - redis-plus-plus - librdkafka # 第三方库构建 拉取项目后,3rdparty文件夹下有个CMakeLists.txt文件,这个是用来一键编译第三方库的,可以实现第三方库的一键编译 ```shell cd 3rdparty mkdir build cd build cmake .. -DCPPSS_BUILD_ALL=ON ``` 编译完成后,如果你没有修改安装路径,将会在项目文件夹下生成install文件夹,所有的第三方库都安装在此目录下 > 之所以不使用系统默认的安装目录,是为了实现多个编译器环境下可以方便的验证,例如我第三方库可以用gcc12编译,然后我再用msvc编译(gcc编译后的install文件夹改名为install-gcc), 这时,我将有两套完整的环境,我服务器程序就可以进行多种编译环境的验证 第三方库可以通过以下参数进行开关 |参数|说明|默认选项| |:-|:-|:-| |CPPSS_BUILD_ALL|Build all existing third-party libraries. This parameter takes effect. The CPPSS_ENABLE_ * parameter will be ignored|ON| |CPPSS_ENABLE_ASIO|enable asio|ON| |CPPSS_ENABLE_SPDLOG|enable spdlog|ON| |CPPSS_ENABLE_NLOHMANN_JSON|enable nlohmann-json|ON| |CPPSS_ENABLE_RAPIDJSON|enable rapidjson|ON| |CPPSS_ENABLE_RDKAFKA|enable rdkafka|ON| |CPPSS_ENABLE_HIREDIS|enable hiredis|ON| |CPPSS_ENABLE_REDIS_PLUSPLUS|enable redis++|OFF| |CPPSS_ENABLE_TINYXML2|enable tinyxml2|ON| |CPPSS_ENABLE_MINI|enable mINI|ON| |CPPSS_ENABLE_CONCURRENTQUEUE|enable concurrentqueue|ON| # 第三方库引入 用cmake打开项目目录下的CMakeLists.txt文件,首先会自动引入已经安装的第三方库,你能看到cmake输出如下信息: ```txt -- hiredis 1.2.0 was installed,you can link hiredis: -- |- target_link_libraries(your_target_name PRIVATE hiredis::hiredis) -- |- hiredis_LIBRARIES=hiredis::hiredis -- |- hiredis_INCLUDE_DIRS=C:/src/cpp/cpp-server-scaffolding/install/include -- redis++ 1.3.13 was installed,you can link redis++ by shared lib: -- |- target_link_libraries(your_target_name PRIVATE redis++::redis++) -- |- or link static lib by: -- |- target_link_libraries(your_target_name PRIVATE redis++::redis++_static) -- nlohmann_json 3.11.3 was installed,you can link nlohmann_json: -- |- target_link_libraries(your_target_name PRIVATE nlohmann_json::nlohmann_json) -- RapidJSON 1.1.0 was installed,you can link RapidJSON: -- |- target_include_directories(your_target_name ${RapidJSON_INCLUDE_DIR}) -- |- RapidJSON_INCLUDE_DIR=C:/src/cpp/cpp-server-scaffolding/install/include/rapidjson -- spdlog 1.14.1 was installed,you can link spdlog: -- |- target_link_libraries(your_target_name spdlog::spdlog) -- |- or use header only by: -- |- target_link_libraries(your_target_name spdlog::spdlog_header_only) -- RdKafka 2.3.0 was installed,you can link RdKafka: -- |- target_link_libraries(your_target_name RdKafka::rdkafka) -- |- or use RdKafka++ by: -- |- target_link_libraries(your_target_name RdKafka::rdkafka++) -- tinyxml2 10.0.0 was installed,you can link tinyxml2: -- |- target_link_libraries(your_target_name tinyxml2::tinyxml2) -- asio was installed,you can link asio: -- |- target_include_directories(your_target_name ${CPPSCAFFOLDING_INCLUDE_DIR}/asio) -- mINI was installed,you can link mINI: -- |- target_include_directories(your_target_name ${CPPSCAFFOLDING_MINI_INCLUDE_DIR}) -- concurrentqueue 1.0.0 was installed,you can link concurrentqueue: -- |- target_link_libraries(your_target_name concurrentqueue::concurrentqueue) -- you can import 3rdparty: => target_link_libraries(your_target_name PRIVATE hiredis::hiredis) => target_link_libraries(your_target_name PRIVATE redis++::redis++) / target_link_libraries(your_target_name PRIVATE redis++::redis++_static) => target_link_libraries(your_target_name PRIVATE nlohmann_json::nlohmann_json) => target_include_directories(your_target_name ${RapidJSON_INCLUDE_DIR}) => target_link_libraries(your_target_name spdlog::spdlog) / target_link_libraries(your_target_name spdlog::spdlog_header_only) => target_link_libraries(your_target_name RdKafka::rdkafka) / target_link_libraries(your_target_name RdKafka::rdkafka++) => target_link_libraries(your_target_name tinyxml2::tinyxml2) => target_include_directories(your_target_name ${CPPSCAFFOLDING_INCLUDE_DIR}/asio) => target_include_directories(your_target_name ${CPPSCAFFOLDING_MINI_INCLUDE_DIR}) => target_link_libraries(your_target_name concurrentqueue::concurrentqueue) -- you can add your source by:add_subdirectory(src) ``` 如果你已经安装好相关的第三方库,按照提示信息即可方便引入对应的库,避免查询文档,针对自己的项目,你需要做的是在src目录下写对应自己工程的cmake文件,并在顶层的cmake文件最后添加`add_subdirectory(src)` # 第三方库说明 第三方库使用git的submodule管理,而不是用cmake的ExternalProject_Add来管理 第三方库的构建使用cmake的ExternalProject_Add来管理,而不是用add_subdirectory ## 第三方库介绍: 项目收录的第三方库是目前常用的第三方库,不包括boost、folly、poco、Abseil这些相对大型的库 ### asio 异步网络框架,header only,不支持cmake,直接include ### hiredis redis支撑 ### redis-plus-plus redis的cpp11封装,此库要单独安装 ### librdkafka kafka的支撑 ### nlohmann-json 一个较为友好的json库 ### rapidjson 一个快速json库 ### mINI ini文件解析库,不支持cmake,header only ### spdlog 一个快速的日志库 ### tinyxml2 一个快速的xml库 ### concurrentqueue 一个无锁的mpmc队列和mpmc blocking队列