DEV Community

David Velho
David Velho

Posted on

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

Top comments (0)