Skip to content

Instantly share code, notes, and snippets.

@artivis
Last active April 22, 2019 15:41
Show Gist options
  • Select an option

  • Save artivis/9a8617f68645f55eb99dd2af9b4b1461 to your computer and use it in GitHub Desktop.

Select an option

Save artivis/9a8617f68645f55eb99dd2af9b4b1461 to your computer and use it in GitHub Desktop.
[CMake] set cpp11-14
cmake_minimum_required(VERSION 2.8.3)
project(my_project)
# assuming this file is shipped within your project
# in 'project_root/cmake/cxx_support.cmake'
include(${PROJECT_SOURCE_DIR}/cmake/cxx_support.cmake)
# Assert the build was triggered with
# cpp11 flag, cash otherwise.
assert_cpp11_support()
...
## Declare a C++ library
add_library(${PROJECT_NAME}
...
)
# Set required C++11 flag
set_cpp11_target(${PROJECT_NAME})
function(assert_cxx_support cxx)
# Check that the compoiler has cxx support
# Crash otherwise.
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++${cxx}" COMPILER_SUPPORTS_CXX${cxx})
if(COMPILER_SUPPORTS_CXX${cxx})
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++${cxx} support.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++${cxx} support. Please use a different C++ compiler.")
endif()
endfunction(assert_cxx_support)
macro(assert_cpp11_support)
assert_cxx_support(11)
endmacro()
macro(assert_cpp14_support)
assert_cxx_support(14)
endmacro()
function(set_cxx_target cxx)
foreach(t ${ARGN})
# Set cxx for target ${a}
if(CMAKE_VERSION VERSION_LESS "3.1")
set_target_properties(${t} PROPERTIES COMPILE_FLAGS "-std=c++${cxx}")
else()
set_property(TARGET ${t} PROPERTY CXX_STANDARD ${cxx})
set_property(TARGET ${t} PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET ${t} PROPERTY CXX_EXTENSIONS OFF)
endif()
endforeach()
endfunction(set_cxx_target)
macro(set_cpp11_target)
set_cxx_target(11 ${ARGV})
endmacro()
macro(set_cpp14_target)
set_cxx_target(14 ${ARGV})
endmacro()
#Global:
#
#cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)
#...
#set(PROPERTY CXX_STANDARD 11) # 98, 11 or 14
#set(PROPERTY CXX_STANDARD_REQUIRED ON)
#set(PROPERTY CXX_EXTENSIONS OFF) # avoid linking to e.g. gnu
#
#
#Per target:
#
#cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)
#...
#set_property(TARGET target1 target2 ... PROPERTY CXX_STANDARD 11) # 98, 11 or 14
#set_property(TARGET target1 target2 ... PROPERTY CXX_STANDARD_REQUIRED ON)
#set_property(TARGET target1 target2 ... PROPERTY CXX_EXTENSIONS OFF)
#
#Per features:
#
#cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)
#...
#target_compile_features(myTarget
# PUBLIC
# cxx_variadic_templates
# cxx_nullptr
# ...
# PRIVATE
# cxx_lambdas
# ...
#)
#The above require cmake 3.1.0/3.2.0
function(set_cxx_target cxx)
foreach(a ${ARGN})
# Set cxx for target ${a}
if(CMAKE_VERSION VERSION_LESS "3.1")
set_target_properties(${a} PROPERTIES COMPILE_FLAGS "-std=c++${cxx}")
else()
set_property(TARGET ${a} PROPERTY CXX_STANDARD ${cxx})
set_property(TARGET ${a} PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET ${a} PROPERTY CXX_EXTENSIONS OFF)
endif()
endforeach()
endfunction(set_c11_target)
macro(set_c11_target)
set_cxx_target(11 ARGN)
endmacro()
macro(set_c14_target)
set_cxx_target(14 ARGN)
endmacro()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment