Skip to content

soerenPeters/cpp_start

Repository files navigation

cpp_start

Gitter CI Quality Gate Status

cpp_start is a cmake - C++ starter project.

Build

$ git clone https://github.com/soerenPeters/cpp_start.git
$ cd cpp_start && mkdir build && cd build
$ cmake ..
$ make
$ ctest

CMake Options

  • BUILD_SHARED_LIBS : Build all targets as shared libraries in this project. (ON)
  • CS_BUILD_WARNINGS_AS_ERRORS : Make all warnings into errors. (OFF)
  • CS_BUILD_UNIT_TESTS : Create unit test targets. (ON)
  • CS_ENABLE_CATCH2 : Fetch and link the unit-Test against catch2. (ON)
  • CS_ENABLE_GTEST : Fetch and link the unit-tests against googletest. (ON)
  • CS_ENABLE_COVERAGE : Add the --coverage compiler flag. (OFF)
  • CS_ENABLE_CLANG_TIDY : Enable clang-tidy checks. (OFF)
  • CS_ENABLE_CPPCHECK : Enable cppcheck. (OFF)
  • CS_ENABLE_INCLUDE_WHAT_YOU_USE: "Enable Include what you use. (OFF)
  • CS_USE_OPENMP : use OpenMP. (OFF)
  • CS_USE_MPI : use MPI. (OFF)

Compiler flags

Compiler flags can be set individually for each compiler in "CMake/compilerflags/CS_COMPILER_FLAGS_CXX .cmake". The following cmake lists will be set as private properties for each target:

  • CS_COMPILER_FLAGS_CXX
  • CS_COMPILER_FLAGS_CXX_DEBUG
  • CS_COMPILER_FLAGS_CXX_RELEASE
  • CS_COMPILER_DEFINITION
  • CS_LINK_OPTIONS

Github workflow:

Set up Sonarcloud:

  • link the github project on sonarcloud.io
  • generate a security token on sonarcloud.io: User > My Account > Security
  • add this token as a github project secret with the name SONAR_TOKEN

Note: sonar-project.properties needs to be adapted individually. Further information can be found here in the sonarcloud docs. To exclude the test files from the sonarcloud code coverage report, the test files must be named after the pattern 'sonar.coverage.exclusions'.

Dependencies

There are dependencies on the following projects. These are fetched into the build order during cmake.

Further dependencies are not included. If they are enable in a cmake option they must be provided within the environment.

  • Boost
  • OpenMP
  • MPI
  • cppcheck
  • clang-tidy
  • lizard

Usage

Add new targets

  • create a new folder in the src/ directory (e.g. by copying the adder directory.)
  • add a new CMakeLists.txt file and the corresponding source and test files
project(project_name CXX)

set(TARGET_NAME
        my_target_name)

set(SOURCE_FILES
        foo.cpp)

set(TEST_FILES
        test_foo.cpp)

set(PUBLIC_LINK)

set(PRIVATE_LINK)

add_target()
  • add an add_subdirectory() in the root CMakeLists.txt

If the option BUILD_UNIT_TESTS is set to ON and TEST_FILES are added in the CMakeLists, cmake will create a test executable target with the name <my_target_name>Test and add it to ctest.

Use in other projects

This cmake template can easily be used in other projects:

include(FetchContent)
FetchContent_Declare(
        cpp_start
        GIT_REPOSITORY https://github.com/soerenpeters/cpp_start.git
        GIT_TAG        v0.1-alpha
)
FetchContent_Populate(cpp_start)

include(${CMAKE_BINARY_DIR}/_deps/cpp_start-src/CMake/cpp_starter.cmake)

An example usage can be found here.