Github地址:https://github.com/filipdutescu/modern-cpp-template
这是一个现代C++的工程模板,帮助我们快速的启动一个项目的开发。
readme.md
以下为项目readme.md文件的中文释义。
特性(Features)
- 使用现代CMake配置与工程
- Clang-Format配置示例
- 集成了静态分析器,包括Clang-Tidy(默认)与Cppcheck
- 支持Doxygen,通过
ENABLE_DOXYGEN
打开 - 支持单元测试,包括GoogleTest(默认,并带有启用
GoogleMock
的选项)或Catch2 - 支持Code coverage,通过
ENABLE_CODE_COVERAGE
启用 - 包管理器,包括Conan与Vcpkg
- 使用GitHub Actions的Windows、Linux与MacOS的CI工作流,利于缓存功能,以确保最短的运行时间
.md
模板,包含自述文件、贡献指南、问题与拉取请求- 允许你很方便的提供Permissive license,该模板在Unlicense下获得许可
- 既可构建为静态库,又可构建为动态库,甚至可构建为仅头文件的库,还可以构建为可执行文件
- 集成了Ccache,用于加速rebuild的时间
开始(Getting Started)
先决条件(Prerequisites)
- CMake v3.15+
- C++ Compiler - needs to support at least the C++17 standard, i.e. MSVC, GCC, Clang
初始化
主要工作就是改工程名,工程名散乱在各个角落,你需要一个一个修改
- 拉取工程:
git clone https://github.com/filipdutescu/modern-cpp-template/
- 在
include/
目录下创建一个新文件夹,如include/<你的工程名>
- 编辑
cmake/SourcesAndHeaders.cmake
添加你的文件 - 对
cmake/ProjectConfig.cmake.in
进行重命名,如cmake/<你的工程名>Config.cmake.in
- 修改Github工作流,特别是
[.github/workflows/ubuntu.yml](https://github.com/filipdutescu/modern-cpp-template/blob/master/.github/workflows/ubuntu.yml)
- 将CMake选项
-DProject_ENABLE_CODE_COVERAGE=1
替换为-D<你的工程名>_ENABLE_CODE_COVERAGE=1
- 将CMake选项
- 最后,修改
CMakeLists.txt
- 将
project("Project" ...)
改成project(<你的工程名> ...)
- 将
构建项目
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=D:/install
cmake --build . --target install
注意:如果您希望安装在 默认安装位置,可以省略自定义。CMAKE_INSTALL_PREFIX
更多选项,可以在文件cmake/StandardSettings.cmake中进行设置。对于某些选项,可能需要在其各自的*cmake
文件中进行配置
- 如,conan的相关设置在
[cmake/Conan.cmake](https://github.com/filipdutescu/modern-cpp-template/blob/master/cmake/Conan.cmake)
中。需要CONAN_REQUIRES
并且可能需要CONAN_OPTIONS
设置才能使其正常工作
生成文档
为了为项目生成文档,您需要将构建配置为使用Doxygen
mkdir build/ && cd build/
cmake .. -D<project_name>_ENABLE_DOXYGEN=1 -DCMAKE_INSTALL_PREFIX=/absolute/path/to/custom/install/directory
cmake --build . --target doxygen-docs
注意: 这将在项目的根目录中生成一个docs/
目录
运行测试
默认情况下使用Google Test进行单元测试。如要使用构建目录中的CTest,传递运行测试所需的配置
cd build # if not in the build directory already
ctest -C Release # or `ctest -C Debug` or any other configuration you wish to test
# you can also run tests with the `-VV` flag for a more verbose output (i.e.
#GoogleTest output as well)
如何关闭单元测试?
ENABLE_UNIT_TESTING
设置为false(在cmake/StandardSettings.cmake中)
安装
若要安装已构建的项目,您需要install
,如
cmake --build build --target install --config Release
# a more general syntax for that command is:
cmake --build <build_directory> --target install --config <desired_config>
相关配置
使用conan
使用conan时,不应该使用conanfile.txt,应该在cmake/Conan.cmake当中设置。例如
# 设置包
set(${PROJECT_NAME}_CONAN_REQUIRES
"cpprestsdk/2.10.18"
"boost/1.76.0"
"libuv/1.41.0"
"spdlog/1.8.5"
"rapidjson/cci.20200410"
"nlohmann_json/3.9.1"
"fmt/7.1.3"
"date/3.0.1"
"tbb/2020.3"
"catch2/2.13.6"
)
设置好之后,需要打开conan,有多种方式
在cmake时添加此参数,但这个太麻烦了,每次构建工程都需要加
cmake .. -D<project_name>_ENABLE_CONAN=1
在
CMakeLists.txt
中添加 ```cmake project( “Project” VERSION 0.1.0 LANGUAGES CXX )
#
Set project options
#
要放在project命令之后
set(${PROJECT_NAME}_ENABLE_CONAN 1)
<a name="RAI9N"></a>
# 使用示例
仓库:[https://github.com/geodoer/modern-cpp-template](https://github.com/geodoer/modern-cpp-template)
启用
- conan
- Googletest(默认开启)
这里不修改工程名,以默认工程名`Project`为例,需要修改工程名,可参考“安装”。
我们使用conan安装googletest,所以要改一些事情
1. 在`cmake/conan.cmake`中添加安装googletest
![image.png](https://cdn.nlark.com/yuque/0/2022/png/1465826/1648108444571-3b1e6abb-1f4b-47f4-93e8-9fde9fd44e20.png#clientId=u39523e5d-1d2d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=310&id=u80810c28&margin=%5Bobject%20Object%5D&name=image.png&originHeight=387&originWidth=665&originalType=binary&ratio=1&rotation=0&showTitle=false&size=31493&status=done&style=none&taskId=u60092d4a-b366-48d6-ad44-9e2bbd3db13&title=&width=532)
2. 在`CMakeLists.txt`中开启conan,并且把编译类型改成Relase(方便conan直接下载Release版本,如果要调试在VS中改用RelWithDebInfo即可)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/1465826/1648110660968-8e616415-f8f7-485e-ac39-bbcc5cb2c8bb.png#clientId=u387b8798-7e07-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=599&id=u654e4e0b&margin=%5Bobject%20Object%5D&name=image.png&originHeight=749&originWidth=871&originalType=binary&ratio=1&rotation=0&showTitle=false&size=63639&status=done&style=none&taskId=ud67e9d4b-5ddd-4703-aab2-fd558d682b3&title=&width=696.8)
3. 在`test/CMakeLists.txt`中替换链接库,链接库为conan安装的lib
![image.png](https://cdn.nlark.com/yuque/0/2022/png/1465826/1648108388670-b4d43db2-af54-4062-ba53-4c3519ba902f.png#clientId=u39523e5d-1d2d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=705&id=u2970e3a1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=881&originWidth=831&originalType=binary&ratio=1&rotation=0&showTitle=false&size=83955&status=done&style=none&taskId=uedfb4967-f05c-49b1-9f21-88de96bc7cb&title=&width=664.8)
4. 然后构建工程即可
```cmake
cmake .. -G "Visual Studio 16" -DCMAKE_INSTALL_PREFIX=D:/install/project
- 打开即可编译通过
- 点击install即可安装
注:官方代码有一个BUG,test工程没有链接include,需要在test/cmakelists.txt加上以下内容