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

初始化

主要工作就是改工程名,工程名散乱在各个角落,你需要一个一个修改

  1. 拉取工程:git clone https://github.com/filipdutescu/modern-cpp-template/
  2. include/目录下创建一个新文件夹,如include/<你的工程名>
  3. 编辑cmake/SourcesAndHeaders.cmake添加你的文件
  4. cmake/ProjectConfig.cmake.in进行重命名,如cmake/<你的工程名>Config.cmake.in
  5. 修改Github工作流,特别是[.github/workflows/ubuntu.yml](https://github.com/filipdutescu/modern-cpp-template/blob/master/.github/workflows/ubuntu.yml)
    1. 将CMake选项-DProject_ENABLE_CODE_COVERAGE=1替换为-D<你的工程名>_ENABLE_CODE_COVERAGE=1
  6. 最后,修改CMakeLists.txt
    1. project("Project" ...)改成project(<你的工程名> ...)

构建项目

  1. mkdir build && cd build
  2. cmake .. -DCMAKE_INSTALL_PREFIX=D:/install
  3. 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

  1. mkdir build/ && cd build/
  2. cmake .. -D<project_name>_ENABLE_DOXYGEN=1 -DCMAKE_INSTALL_PREFIX=/absolute/path/to/custom/install/directory
  3. cmake --build . --target doxygen-docs

注意: 这将在项目的根目录中生成一个docs/目录

运行测试

默认情况下使用Google Test进行单元测试。如要使用构建目录中的CTest,传递运行测试所需的配置

  1. cd build # if not in the build directory already
  2. ctest -C Release # or `ctest -C Debug` or any other configuration you wish to test
  3. # you can also run tests with the `-VV` flag for a more verbose output (i.e.
  4. #GoogleTest output as well)

如何关闭单元测试?

安装

若要安装已构建的项目,您需要install,如

  1. cmake --build build --target install --config Release
  2. # a more general syntax for that command is:
  3. cmake --build <build_directory> --target install --config <desired_config>

相关配置

使用conan

使用conan时,不应该使用conanfile.txt,应该在cmake/Conan.cmake当中设置。例如

  1. # 设置包
  2. set(${PROJECT_NAME}_CONAN_REQUIRES
  3. "cpprestsdk/2.10.18"
  4. "boost/1.76.0"
  5. "libuv/1.41.0"
  6. "spdlog/1.8.5"
  7. "rapidjson/cci.20200410"
  8. "nlohmann_json/3.9.1"
  9. "fmt/7.1.3"
  10. "date/3.0.1"
  11. "tbb/2020.3"
  12. "catch2/2.13.6"
  13. )

设置好之后,需要打开conan,有多种方式

  1. 在cmake时添加此参数,但这个太麻烦了,每次构建工程都需要加

    1. cmake .. -D<project_name>_ENABLE_CONAN=1
  2. CMakeLists.txt中添加 ```cmake project( “Project” VERSION 0.1.0 LANGUAGES CXX )

#

Set project options

#

要放在project命令之后

set(${PROJECT_NAME}_ENABLE_CONAN 1)

  1. <a name="RAI9N"></a>
  2. # 使用示例
  3. 仓库:[https://github.com/geodoer/modern-cpp-template](https://github.com/geodoer/modern-cpp-template)
  4. 启用
  5. - conan
  6. - Googletest(默认开启)
  7. 这里不修改工程名,以默认工程名`Project`为例,需要修改工程名,可参考“安装”。
  8. 我们使用conan安装googletest,所以要改一些事情
  9. 1. 在`cmake/conan.cmake`中添加安装googletest
  10. ![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)
  11. 2. 在`CMakeLists.txt`中开启conan,并且把编译类型改成Relase(方便conan直接下载Release版本,如果要调试在VS中改用RelWithDebInfo即可)
  12. ![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)
  13. 3. 在`test/CMakeLists.txt`中替换链接库,链接库为conan安装的lib
  14. ![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)
  15. 4. 然后构建工程即可
  16. ```cmake
  17. cmake .. -G "Visual Studio 16" -DCMAKE_INSTALL_PREFIX=D:/install/project
  1. 打开即可编译通过
  2. 点击install即可安装

image.png
image.png

注:官方代码有一个BUG,test工程没有链接include,需要在test/cmakelists.txt加上以下内容
image.png