5

I want set compiler C++ standard to gnu++17 but I cant do that with CMAKE_CXX_STANDARD as it gives me gnu++17 is invalid value.Thus I put it in CMAKE_CXX_FLAGS . But when I compile cmake appends std=gnu++11 to the end of command like this:-

/bin/g++ -Wall -Wpedantic -Wextra -std=gnu++14 -no-pie -fPIC -std=gnu++11 -o /path/to/main.cpp.o -c /blah/blahfeeelk 

thus std=gnu++11 win . So want to append gnu++17 to the end . How can I do that ? (I have trimmed the original command)

1 Answer 1

6

CMake has three variables to control the version of the C++ standard and switching compiler extensions on/off. I’m showing the target specific versions here because that is how it should be used in reasonably modern (v3.x) CMake.

add_executable(foo main.cpp) set_target_properties(foo PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS ON ) 

This enables C++17, prevents automatic fallback to an earlier standard if 17 is not available, and enables compiler extensions. For GCC this is equivalent to -std=gnu++17 or -std=gnu++1z.

CXX_STANDARD with the 17 value is available since CMake 3.8. CXX_STANDARD_REQUIRED and CXX_EXTENSIONS exist since CMake 3.1.

Sign up to request clarification or add additional context in comments.

1 Comment

This only works for std versions whitelisted by cmake. It does NOT work for c++2a. A solution for this would be nice (since something keeps adding -std=gnu++11 flag in my case)