[CMake] C++11 and try_compile

Sascha Zelzer s.zelzer at dkfz-heidelberg.de
Thu Mar 12 13:35:14 EDT 2015


Hi,

tldr; In a C++11 enabled CMake project, is try_compile() supposed to 
*not* use -std=c++11 by default?

--- Long(er) version

If I use a minimal CMakeLists.txt file like the one below with gcc and 
try_compile() compiles a source file containing C++11 features, I would 
naively assume that try_compile() uses the -std=c++11 flag because 
CMAKE_CXX_STANDARD_REQUIRED is set to ON. However, this is not the case 
(tested up to CMake 3.2.1).

The C++11 language standard and feature support in CMake is awesome, but 
in order to get correct results for commands like try_compile, 
check_cxx_compiler_flag, etc. I still need to manually set -std=c++11 to 
CMAKE_FLAGS or similar command arguments / variables.

Is there some policy which could be set or a concrete reason for the 
described behavior? How do people deal with try_compile checks in C++11 
enabled projects?

Thanks,

Sascha

------------

cmake_minimum_required(VERSION 3.2.1)

project(cxx_test)

include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED 1)
set(CMAKE_CXX_EXTENSIONS 0)

try_compile(TRY_COMPILE_SUCCESS ${CMAKE_CURRENT_BINARY_DIR}
             ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
if(NOT TRY_COMPILE_SUCCESS)
   message(WARNING "try_compile failed")
endif()

file(READ main.cpp source)
check_cxx_source_compiles("${source}" COMPILE_SUCCESS)
if(NOT COMPILE_SUCCESS)
   message(WARNING "check_cxx_source_compiles failed")
endif()

check_cxx_compiler_flag(-fconstexpr-depth=4 FLAG_SUCCESS)
if(NOT FLAG_SUCCESS)
   message(WARNING "check_cxx_compiler_flag failed")
endif()

add_executable(cxx_test main.cpp)




More information about the CMake mailing list