5

I'm new to cmake, and I was building some c++11 code with it (notably a set of template aliases.) I want to use the CXX_STANDARD property to hopefully cover all platforms and problems introduced by simply adding -std=c++11 to cxxflags, which worked for me before:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

But when I change this to

set_property(GLOBAL PROPERTY CXX_STANDARD 11)
set_property(GLOBAL PROPERTY CXX_STANDARD_REQUIRED true)

cmake doesn't tell the compiler to use c++11. What am I doing wrong with the latter code?

ralian
  • 113
  • 6
  • May be [this](http://stackoverflow.com/questions/10984442/how-to-detect-c11-support-of-a-compiler-with-cmake) will help you? – ha9u63a7 Jun 11 '15 at 23:02
  • Thanks, I had already seen that, but on inspecting it more carefully I realized that the more or less valid code I produced above needs at least cmake version 3.1, where I was running version 2.8. I upgraded and it works fine. – ralian Jun 11 '15 at 23:54

2 Answers2

6

I didn't have CMake updated to the latest version... I should have done that before posting a question. The code above only works for version > 3.1.

ralian
  • 113
  • 6
4

CMAKE_CXX_STANDARD is not a global property, but a variable. http://www.cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html#variable:CMAKE_CXX_STANDARD

So all you need is

set( CMAKE_CXX_STANDARD 11 )

before you define any targets.

Finn
  • 1,018
  • 6
  • 6