使用前需把 CMakeLists.txt 里的 project 名、cmake/ProjectConfig.cmake.in 文件名和 CI 工作流中的选项前缀改成自己的项目名;启用 Doxygen 需显式加 -D<project_name>_ENABLE_DOXYGEN=1;用 Conan 时还需在 cmake/Conan.cmake 里配置 CONAN_REQUIRES 等选项
modern-cpp-template
filipdutescuUnlicense更新放缓开箱即用的现代 C++ 项目脚手架,集成 CMake、Clang-Format、CI、单元测试和代码覆盖率,省去从零搭建构建流程的麻烦。
A template for modern C++ projects using CMake, Clang-Format, CI, unit testing and more, with support for downstream inclusion.
git clone https://github.com/filipdutescu/modern-cpp-template/README 未说明,仅要求 CMake v3.15+ 和支持 C++17 的编译器(MSVC、GCC、Clang)
现代 CMake C++ 项目脚手架,帮你一次性配好格式化、静态分析、单测和 CI
开箱即用的现代 C++ 项目脚手架,集成 CMake、Clang-Format、CI、单元测试和代码覆盖率,省去从零搭建构建流程的麻烦。
核心能力
- 提供现代 CMake 项目配置与最佳实践示例
- 集成 Clang-Format 与 Clang-Tidy、Cppcheck 静态分析
- 用 GoogleTest 或 Catch2 做单元测试并接入 Codecov 覆盖率
- 内置 Windows、Linux、macOS 的 GitHub Actions CI 工作流
- 支持 Conan 与 Vcpkg 包管理器及 Ccache 加速重编译
容易踩的坑
- v4.1.0Release v4.1.0最新正式版2021-10-07 · 2021-10-07 19:06 UTC
Features
- Add a
_DEBUGmacro for Unix systems, alongside the MSVC one (GH-31)
- Add a
- v4.0.1Release v4.0.12021-10-04 · 2021-10-04 17:54 UTC
查看更新说明
Fixes
- Verbose output now prints the sources and headers found (GH-29).
- v4.0.0Release v4.0.02021-09-30 · 2021-09-30 18:15 UTC
查看更新说明
Fixes
- Fixed the default
cmake/Conan.cmakefile to work out of the box (#28)
- Fixed the default
- v3.1.0Release v3.1.02021-08-20 · 2021-08-20 19:48 UTC
查看更新说明
Fixes:
- MSVC runtime library not being setup properly (#24)
- Removed
${PROJECT_NAME}_BUILD_SHARED_LIBSin favour of the standard CMakeBUILD_SHARED_LIBS(#26)
- v3.0.0Release v3.0.02021-08-05 · 2021-08-05 21:59 UTC
查看更新说明
Various bug fixes
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Modern C++ Template
A quick C++ template for modern CMake projects, aimed to be an easy to use starting point.
This is my personal take on such a type of template, thus I might not use the best practices or you might disagree with how I do things. Any and all feedback is greatly appreciated!
Features
-
Modern CMake configuration and project, which, to the best of my knowledge, uses the best practices,
-
An example of a Clang-Format config, inspired from the base Google model, with minor tweaks. This is aimed only as a starting point, as coding style is a subjective matter, everyone is free to either delete it (for the LLVM default) or supply their own alternative,
-
Static analyzers integration, with Clang-Tidy and Cppcheck, the former being the default option,
-
Doxygen support, through the
ENABLE_DOXYGENoption, which you can enable if you wish to use it, -
Unit testing support, through GoogleTest (with an option to enable GoogleMock) or Catch2,
-
Code coverage, enabled by using the
ENABLE_CODE_COVERAGEoption, through Codecov CI integration, -
Package manager support, with Conan and Vcpkg, through their respective options
-
CI workflows for Windows, Linux and MacOS using GitHub Actions, making use of the caching features, to ensure minimum run time,
-
.md templates for: README, Contributing Guideliness, Issues and Pull Requests,
-
Permissive license to allow you to integrate it as easily as possible. The template is licensed under the Unlicense,
-
Options to build as a header-only library or executable, not just a static or shared library.
-
Ccache integration, for speeding up rebuild times
Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
This project is meant to be only a template, thus versions of the software used can be change to better suit the needs of the developer(s). If you wish to use the template as-is, meaning using the versions recommended here, then you will need:
-
CMake v3.15+ - found at https://cmake.org/
-
C++ Compiler - needs to support at least the C++17 standard, i.e. MSVC, GCC, Clang
Note: You also need to be able to provide CMake a supported generator.
Installing
It is fairly easy to install the project, all you need to do is clone if from GitHub or generate a new repository from it (also on GitHub).
If you wish to clone the repository, rather than generate from it, you simply need to run:
git clone https://github.com/filipdutescu/modern-cpp-template/
After finishing getting a copy of the project, with any of the methods above, create
a new folder in the include/ folder, with the name of your project. Edit
cmake/SourcesAndHeaders.cmake to add your files.
You will also need to rename the cmake/ProjectConfig.cmake.in file to start with
the exact name of your project. Such as cmake/MyNewProjectConfig.cmake.in.
You should also make the same changes in the GitHub workflows provided, notably
.github/workflows/ubuntu.yml, in which you should
replace the CMake option -DProject_ENABLE_CODE_COVERAGE=1 to
-DMyNewProject_ENABLE_CODE_COVERAGE=1.
Finally, change "Project" from CMakeLists.txt, from
project(
"Project"
VERSION 0.1.0
LANGUAGES CXX
)
to the exact name of your project, i.e. using the previous name it will become:
project(
MyNewProject
VERSION 0.1.0
LANGUAGES CXX
)
To install an already built project, you need to run the install target with CMake.
For example:
cmake --build build --target install --config Release
# a more general syntax for that command is:
cmake --build <build_directory> --target install --config <desired_config>
Building the project
To build the project, all you need to do, after correctly installing the project, is run a similar CMake routine to the the one below:
mkdir build/ && cd build/
cmake .. -DCMAKE_INSTALL_PREFIX=/absolute/path/to/custom/install/directory
cmake --build . --target install
Note: The custom
CMAKE_INSTALL_PREFIXcan be omitted if you wish to install in the default install location.
More options that you can set for the project can be found in the
cmake/StandardSettings.cmake file. For certain
options additional configuration may be needed in their respective *.cmake files
(i.e. Conan needs the CONAN_REQUIRES and might need the CONAN_OPTIONS to be setup
for it work correctly; the two are set in the cmake/Conan.cmake file).
Generating the documentation
In order to generate documentation for the project, you need to configure the build to use Doxygen. This is easily done, by modifying the workflow shown above as follows:
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
Note: This will generate a
docs/directory in the project's root directory.
Running the tests
By default, the template uses Google Test
for unit testing. Unit testing can be disabled in the options, by setting the
ENABLE_UNIT_TESTING (from
cmake/StandardSettings.cmake) to be false. To run
the tests, simply use CTest, from the build directory, passing the desire
configuration for which to run tests for. An example of this procedure is:
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)
End to end tests
If applicable, should be presented here.
Coding style tests
If applicable, should be presented here.
Contributing
Please read CONTRIBUTING.md for details on our how you can become a contributor and the process for submitting pull requests to us.
Versioning
This project makes use of SemVer for versioning. A list of existing versions can be found in the project's releases.
Authors
- Filip-Ioan Dutescu - @filipdutescu
License
This project is licensed under the Unlicense - see the LICENSE file for details