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)


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to