This is a template for a C++ project. It is based on the project gui_starter_template but avoids using package managers.
The goal is to support three types of usage:
- Use find_package to find the library if it is installed
- Use add_subdirectory to add the library to your project, if you want to add the source code to your project
- Use FetchContent to download the library and use it in your project
cpp_proj_template
│
├── benchmark # Downloads and builds the benchmark library
│ ├── ... # Benchmark cpp files
│ └── CMakeLists.txt
│
├── cmake # CMake modules
│ ├── Config.cmake.in # Config file for find_package. When installing the project, this file is modified and copied to the install directory.
│ ├── CPM.cmake # OPTIONAL Use CPM to download and build dependencies in a shared directory across different projects.
│ ├── cppcheck.cmake # OPTIONAL Use cppcheck per target to check the code for errors
│ ├── setup_doxygen.cmake # OPTIONAL Find the doxygen executable and configure the doxygen documentation
│ ├── project_preamble.cmake # Generic options to be used in all projects
│ ├── setup_sccache.cmake # OPTIONAL Looks for sccache to speed up the compilation
│ ├── setup_exeport_set_installation.cmake # Function that configures the installation of the project targets
│ ├── static_analysis.cmake # OPTIONAL Configures clang-tidy project wide
│ └── warnings.cmake # OPTIONAL Configures warnings per target
│
├── dependencies # Dependency sources are downloaded using FetchContent if not found in the CPM cache directory
│ ├── fmt
│ │ └── CMakelists.txt
│ ├── googletest
│ │ └── CMakelists.txt
│ └── CMakelists.txt
│
├── src # Source files of this project
│ ├── example_executable # example executable that uses example_library
│ │ ├── main.cpp
│ │ └── CMakelists.txt
│ │
│ ├── example_library_src # example_library files. Library may be build as static or shared library.
│ │ │ #
│ │ │ # The idea of this example lib is to print a message to the console
│ │ │ # using the header-only supporting lib, while not exposing the header-only lib to the user.
│ │ ├── example_library
│ │ │ └── library.h
│ │ ├── source
│ │ │ └── library.cpp
│ │ └── CMakelists.txt
│ │
│ └── CMakelists.txt
│
├── test # Test files
│ ├── test_install # Tests the installed library targets using find_package()
│ │ ├── template_find_package_install_test
│ │ └── CMakeLists.txt
│ ├── using_google_test # Tests the library by using the targets of the build directory
│ └── CMakelists.txt
│
├── .clang-format # specifies the formatting style
├── .clang-tidy # specifies the clang-tidy checks
├── .gitignore
└── CMakelists.txt
Requirements
- Visual Studio 16, 64bit or older
- CMake 3.21 or older
- Git
Requirements
- Clang or GCC
- CMake 3.21 or older
- Git
- Ninja
Take a look at the example build scripts for windows (.bat) and ubuntu (.sh) in the main directory or use the commands below.
cmake -B <path-to-build-dir> -S <path-to-source-dir> -G "Visual Studio 16 2019" -A x64 -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20
cmake -B <path-to-build-dir> -S <path-to-source-dir> -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20
cmake --build <path-to-build-dir> --target <target-name> --config Release --parallel
cmake --build <path-to-build-dir> --config Release --parallel
cmake --install <path-to-build-dir> --config Release --prefix <path-to-install-dir>
Example projects
- cpp-best-practices/gui_starter_template
- aminya/project_options
- gitlab.io/modern-cmake/
- Modern-CMake-for-Cpp
Talks:
- C++Now 2017: Daniel Pfeifer “Effective CMake"
- More Modern CMake - Deniz Bahadir - Meeting C++ 2018
- Deep CMake for Library Authors - Craig Scott - CppCon 2019
Books: