简体   繁体   中英

GCC 11: How to tell CMake I don't want the default C++ GNU extensions?

So, back in GCC 10.2 (which used C++14 as the default), I could use this to tell CMake I wanted -std=gnu++17 :

target_compile_features(mytarget PRIVATE cxx_std_17)
set_target_properties(mytarget PROPERTIES
    CXX_STANDARD_REQUIRED ON
)

...which I actually didn't want, so I used CXX_EXTENSIONS OFF to force -std=c++17 :

target_compile_features(mytarget PRIVATE cxx_std_17)
set_target_properties(mytarget PROPERTIES
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF  # -std=c++17 instead of gnu++17
)

Enter GCC 11.1. The default now is the 2017 version of the ISO C++ standard, so that the first snippet above doesn't generate any -std flag for GCC, which is compatible with GCC 11's man page:

c++17 : The 2017 ISO C++ standard plus amendments.

gnu++17 : GNU dialect of -std=c++17 . This is the default for C++ code.

The problem is that the second CMake snippet above also doesn't generate any -std flag . How do I do it now, is this a bug in CMake? How to I tell it I want an explicit -std=c++17 flag?

Remark 1: I know I can just stick -std=c++17 in target_compile_options , but I don't want to do that, do I?

Remark 2: Sanity check: if I change cxx_std_17 to cxx_std_20 , then CXX_EXTENSIONS OFF correctly switches from -std=gnu++2a to -std=c++2a .

The answer to this question is that it is a bug in CMake 3.21 (but technically not 3.20, which is older than the compiler in question) that will be fixed in 3.22, most likely by the following merge request: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6177

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM