[CMake] detecting if c++11 available

Benjamin Eikel cmake at eikel.org
Wed Feb 20 03:07:46 EST 2013


Hello Brad,

Am Mittwoch, 20. Februar 2013, 00:36:18 schrieb Brad Bell:
> My goal is to use some new c++11 features if they are available,
> otherwise to stick to the c++03 features. I need to build test programs
> that check for correctness as well as distribute an include file library.
> 
> Is there a way in cmake to detect if c++11 is available and to use it
> when it is available ? I would like this to be system and compiler
> independent.
> 

I am using the following code:

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
	set_property(TARGET MyTarget APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++11 ")
elseif(COMPILER_SUPPORTS_CXX0X)
	set_property(TARGET MyTarget APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++0x")
else()
	message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

Kind regards
Benjamin

> 
> 
> --
> 
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list