简体   繁体   中英

How to set compiler specific flags in cmake

I have a code that needs to run with MSVC as well as g++. Therefore I use Cmake. I want to use designated initializers which are part C++20. Since they exist since C99 they are already in g++ but in MSVC++ they are only available with the flag /std:latest .

What's the best way to enable this option for MSVC in a CMake file?

You can set the variables CMAKE_CXX_FLAGS_RELEASE (applies to release builds only), CMAKE_CXX_FLAGS_DEBUG (applies to debug builds only) and CMAKE_CXX_FLAGS (applies to both release and debug). In case you use also other compilers, you should only set the options for msvc:

if(MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:latest")
endif()

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