Cmake won't set the compiler standard to c++11

2019-02-22 10:57发布

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?

标签: cmake
2条回答
太酷不给撩
2楼-- · 2019-02-22 11:36

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.

查看更多
疯言疯语
3楼-- · 2019-02-22 11:36

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.

查看更多
登录 后发表回答