A small c++ template with modern CMake

Overview

C++/CMake modern boilerplate

Travis build Status Appveyor build status Coverage Codacy Badge Language grade: C/C++ CDash dashboard

Pull requests Opened issues Documentation

This is a template for new projects, gives a good CMake base and a few dependencies you most likely want in your project. It also set ups some basic CI builds.

It uses "modern" CMake, ie 3.x paradigms, and should be a good starting point for both people willing to learn it and those that want to update/upgrade their CMakeLists.txt!

Everything will not necessarily be useful for new projects, but serves as a learning document where most of the CMake features you will need should be showcased.

If you disagree with some pieces of advice given here, please discuss it with me by opening a Github Issue! Enhancements are always welcome.

Usage

If you want to bootstrap a new project you only need to :

  • If you don't already have your git repository setup
    • Simply copy/paste the folder (without the .git folder) and run the createBoilerPlate.sh file. This will create an initial git commit and add the required submodules.
  • Hack CMakeLists.txt and CTestConfig.cmake to change the project name, remove unnecessary parts/comments.
  • Ready to go !

The CI providers used and that might need some setup :

  • Github actions (no setup required)
  • Travis CI, for GCC / Clang on Linux/MacOS
  • AppVeyor, for MSVC on Windows
  • Codecov.io, for the codecoverage reports
  • CDash, for test and coverage reports using CTest. Can also be used to build nightlies.

Requirements :

  • CMake 3.8.2 (Not needed for all scripts)
  • Git (for the submodules)
  • Any of the CI providers listed above if needed.

Some features/notes :

  • Scripts lying in the cmake/ folder can be copy/pasted for use in any CMake project

  • Uses c++14

  • CopyDllsForDebug.cmake script : A small wrapper around fixup_bundle to copy DLLs to the output directory on windows

  • LTO.cmake script : Easier link time optimization configuration (should work on all CMake 3.x versions) as it used to be painful to setup.

  • Warnings.cmake script : A wrapper around common warning settings

  • Basic unit-testing using doctest

  • Coverage.cmake : Test coverage script to add a 'Coverage' build type to CMake

  • Generating the documentation through TravisCI requires that you setup a Github Token, see https://docs.travis-ci.com/user/deployment/pages/ .

  • The lgtm.com website already knows about cmake and can build most of the projects without any special configuration. A sample configuration is in this project to show how to tag files and disable unneeded warnings. The external folder is automatically recognized and files are tagged as library.

    FAQ

Q: I'm new to this CMake stuff, where do I start ?

A: I would suggest reading CGold which covers this way better than I could.


Q: Why can't I link some new libraries I put inside the external folder ?

A: By default targets are not at the GLOBAL scope, which means your CMakelists.txt might not see it. In this case you can either add an alias/imported library or use find_package/library as you would if the library was not in your buildtree.


Q: Should I always put my dependencies in the folder external

A: Absolutely not ! It is a great place for small libraries, but you probably don't want to have to rebuild big libs every time. For those, you can use a package manager such as Hunter or simply rely on find_package/library.


Q: I don't understand why you made the choice of XXXXXX here ?

A: Open a new issue !

External dependencies (using submodules)

Those dependencies can be easily removed by changing the external/CMakelists.txt and cleaning main.cpp.

  • libfmt In my opinion the best formating library
  • spdlog A logging library based on libfmt
  • doctest A test library not as heavy as the others
Comments
  • Not clear how to enable/disable specific warnings

    Not clear how to enable/disable specific warnings

    I'm looking at the file Warnings.cmake. From the syntax of the target_set_warnings() function it defines, it seems to allow you to disable or enable specific warnings, not just ALL or Annoying; yet - I can't see where in the code this is handled. It seems like it isn't.

    Am I missing something? Is this intentional?

    opened by eyalroz 7
  • Enabling C++11 and later features

    Enabling C++11 and later features

    I've been following the advice given here:

    https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/

    Why in particular do you prefer target_compile_features(PRIVATE cxx_std_YY)?

    Thanks for the example! I'm doing many of the same things, I haven't worked out how you're doing Doxygen, but I do have integrated tests.

    opened by acgetchell 3
  • Running the build and documentation for it

    Running the build and documentation for it

    Hi! I'm quite new to cmake, but I tried to run cmake . after bootstrapping the project. It showed an error from here:

    if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) message(FATAL_ERROR "Do not build in-source. Please remove CMakeCache.txt and the CMakeFiles/ directory. Then build out-of-source.") endif()

    Is there any installation instructions of how to build?

    opened by evenstensberg 2
  • Add a way to enable and disable specific warnings

    Add a way to enable and disable specific warnings

    These changes lets the user enable or disable more warnings. Also added a bit of documentation.

    Usage example :

    target_set_warnings(BoilerPlate ENABLE ALL AS_ERROR ALL)
    if(MSVC)
        target_set_warnings(BoilerPlate ENABLE 4287)
    else()
        target_set_warnings(BoilerPlate ENABLE cast-align conversion useless-cast DISABLE missing-prototypes)
    endif()
    

    Might want to change target_set_warnings to target_warnings

    Should fix #13

    opened by Lectem 2
  • CMakeLists.txt file for external packages                                             │

    CMakeLists.txt file for external packages │

    So I pulled a fresh git copy and when run cmake, it says /external/fmt and /external/spdlog does not contain a CMakeLists.txt file.

    is that expected?

    opened by shelper 1
  • Add coverage reports using CTest+travis+codecov.io

    Add coverage reports using CTest+travis+codecov.io

    Notes:

    • external folder is ignored
    • include(CTest) adds targets so that we don't need to have a CTest script, will need to add a gcov/llvm-cov detection based on compiler used by project ?
    • codecov.io runs gcov (with wrong version) by default, disabled (-X gcov) since we run it with CTest
    • Lots of refactoring and enhancements

    Fixes #6

    opened by Lectem 1
  • Update dependencies and CI

    Update dependencies and CI

    Dependencies have not been updated for a while, it does not really change anything cmake or CI related but it would be good to use the latest versions of libraries.

    enhancement 
    opened by Lectem 0
  • Add automatic coverage support

    Add automatic coverage support

    There seems to be quite a few ways to do coverage, using CTest, by copying targets and adding the coverage flags, using a seperate build directory... Still need to investigate but https://codecov.io or https://coveralls.io/ will be used.

    enhancement help wanted 
    opened by Lectem 0
  • Make the cmake warning module create a target

    Make the cmake warning module create a target

    replace target_set_warnings by a new INTERFACE target (generated trough add_warnings_interface ?) that you can link. This will give a better control (can use PUBLIC/PRIVATE/INTERFACE when linking)

    opened by Lectem 0
Owner
Clément Grégoire
Clément Grégoire
Make CMake less painful when trying to write Modern Flexible CMake

Izzy's eXtension Modules IXM is a CMake library for writing Modern flexible CMake. This means: Reducing the amount of CMake written Selecting reasonab

IXM 107 Sep 1, 2022
A CMake starter template using CPM

Cmake Starter About A lightweight Cmake project meant for a binary application (not a shared library), tests with catch2 are configured, CPM is the pa

Matt Williams 1 Jul 14, 2022
A template C++ repository, using CMake and Catch

C++ Project Template This is a template project for C++. It uses CMake to build and Catch for unit tests. It is integrated with Travis CI, and builds

Joshua Peterson 49 Oct 23, 2022
An OS-agnostic C++ library template in plain CMake.

?? How to export C++ library This repository provides an OS-agnostic C++ library template with plain CMake files with the following features: distribu

Robotology 302 Dec 17, 2022
A minimal CMake template for Qt 5 & 6 projects

Minimal CMake Template for Qt 6 Projects This project is updated for Qt 6. Visit qt5 branch if you are looking for the Qt 5 template. This is a minima

Vincent Lee 180 Sep 21, 2022
Template for reliable, cross-platform C++ project setup using cmake.

The C++ CMake Project Template cmake-init is a sophisticated copy & paste template for modern C and C++ projects. The main goals include support of al

CG Internals 811 Jan 3, 2023
CMake checks cache helper modules – for fast CI CMake builds!

cmake-checks-cache Cross platform CMake projects do platform introspection by the means of "Check" macros. Have a look at CMake's How To Write Platfor

Cristian Adam 65 Dec 6, 2022
CMake scripts for painless usage of SuiteSparse+METIS from Visual Studio and the rest of Windows/Linux/OSX IDEs supported by CMake

CMake scripts for painless usage of Tim Davis' SuiteSparse (CHOLMOD,UMFPACK,AMD,LDL,SPQR,...) and METIS from Visual Studio and the rest of Windows/Lin

Jose Luis Blanco-Claraco 395 Dec 24, 2022
cmake-font-lock - Advanced, type aware, highlight support for CMake

cmake-font-lock - Advanced, type aware, highlight support for CMake

Anders Lindgren 39 Oct 2, 2022
cmake-avr - a cmake toolchain for AVR projects

cmake-avr - a cmake toolchain for AVR projects Testing the example provided The toolchain was created and tested within the following environment: Lin

Matthias Kleemann 163 Dec 5, 2022
CMake module to enable code coverage easily and generate coverage reports with CMake targets.

CMake-codecov CMake module to enable code coverage easily and generate coverage reports with CMake targets. Include into your project To use Findcodec

HPC 82 Nov 30, 2022
unmaintained - CMake module to activate certain C++ standard, feature checks and appropriate automated workarounds - basically an improved version of cmake-compile-features

Compatibility This library provides an advanced target_compile_features() and write_compiler_detection_header(). The problem with those is that they a

Jonathan Müller 74 Dec 26, 2022
[CMake] [BSD-2] CMake module to find ICU

FindICU.cmake A CMake module to find International Components for Unicode (ICU) Library Note that CMake, since its version 3.7.0, includes a FindICU m

julp 29 Nov 2, 2022
Tutorial/Example to deal with modern cmake.

modern_cmake Tutorial/Example to deal with modern cmake. This tutorial assume that you already know how to write a CMakeLists.txt Introduction This re

Marc Schweitzer 24 Nov 14, 2022
A template for projects using both libPeConv and MS Detours

A CMake template for projects using MS Detours along with libPeConv.

hasherezade 14 Dec 16, 2022
CMake project for BL602 RISC-V processor

bl602_cmake_base CMake project for BL602 RISC-V processor How to build NOTE : This project uses a pre-compiled version of the Buffalo SDK (bl_iot_sdk)

null 9 Jan 6, 2022
A CMake toolchain file for iOS, macOS, watchOS & tvOS C/C++/Obj-C++ development

A CMake toolchain file for iOS, macOS, watchOS & tvOS C/C++/Obj-C++ development

Alexander Widerberg 1.4k Jan 4, 2023
curl cmake module libcurl build with msvc x86

curl-msvc Infomation curl cmake module libcurl build with MSVC10.0 arch (x86 | i386) source from https://github.com/curl/curl tags: curl-7_79_1 Usage

Jason Payne 0 May 16, 2022
NeoWorld is a resampler using the CMake build system

NeoWorld is a resampler using the CMake build system. It's designed for utsu, OpenUTAU, and UTAU.

null 5 Dec 23, 2022