DEV Community

David Velho
David Velho

Posted on

1

Include custom CMake modules

Lets abstract some configuration of our project to a user defined Config.cmake file.

Let's assume your project uses CMake and has a directory structure similar to the one shown below

C++ project with directory structure with custom CMake modules

The Config.cmake file has the following stuff -

set_target_properties(
  ${CMAKE_PROJECT_NAME}
  PROPERTIES CXX_STANDARD 17
             CXX_STANDARD_REQUIRED YES
             CXX_EXTENSIONS NO)

include(CheckIPOSupported)
check_ipo_supported(RESULT result)
if(result)
  set_target_properties(${CMAKE_PROJECT_NAME}
                        PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
  set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
  set(CMAKE_CUDA_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") # CMake 3.9+
endif()

Enter fullscreen mode Exit fullscreen mode

Then, in our root CMakeLists.txt file, lets add the following lines -

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules")
Enter fullscreen mode Exit fullscreen mode

This line must go after your project configuration

And finally,

include(Config)
Enter fullscreen mode Exit fullscreen mode

Where Config is the name of file with our abstractions

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more