1. cmake設(shè)置C++標(biāo)準(zhǔn)
cmake有如下一些方式設(shè)置C++標(biāo)準(zhǔn):
1.1 CMAKE_CXX_FLAGS 方式:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
1.2 CXX_STANDARD 方式:
設(shè)置單個(gè)目標(biāo)的C++標(biāo)準(zhǔn)
add_executable(myapp main.cpp)
set_property(TARGET myapp PROPERTY CXX_STANDARD 17)
set_property(TARGET myapp PROPERTY CXX_STANDARD_REQUIRED ON)
設(shè)置全局的C++標(biāo)準(zhǔn)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
其他未經(jīng)測(cè)試的方式暫不列出抬闷,后續(xù)測(cè)試完成之后再更新
2. 代碼判斷C++標(biāo)準(zhǔn)
代碼可如下判斷編譯器支持的是哪個(gè)標(biāo)準(zhǔn)
2.1 對(duì)于GCC和Clang
對(duì)于GCC和Clang辛孵,使用 __cplusplus 宏來(lái)判斷
#if __cplusplus >= 201703L
std::cout << "Compiler supports C++17 or later." << std::endl;
#else
std::cout << "Compiler does not support C++17 or later." << std::endl;
#endif
#if __cplusplus >= 201402L
std::cout << "Compiler supports C++14 or later." << std::endl;
#else
std::cout << "Compiler does not support C++14 or later." << std::endl;
#endif
#if __cplusplus >= 201103L
std::cout << "Compiler supports C++11 or later." << std::endl;
#else
std::cout << "Compiler does not support C++11 or later." << std::endl;
#endif
2.2 對(duì)于Visual Studio
對(duì)于Visual Studio,使用 _MSVC_LANG 宏來(lái)判斷
#if _MSVC_LANG >= 201703L
std::cout << "Compiler supports C++17 or later." << std::endl;
#else
std::cout << "Compiler does not support C++17 or later." << std::endl;
#endif
#if _MSVC_LANG >= 201402L
std::cout << "Compiler supports C++14 or later." << std::endl;
#else
std::cout << "Compiler does not support C++14 or later." << std::endl;
#endif
#if _MSVC_LANG >= 201103L
std::cout << "Compiler supports C++11 or later." << std::endl;
#else
std::cout << "Compiler does not support C++11 or later." << std::endl;
#endif
在vs編譯器中測(cè)試時(shí)發(fā)現(xiàn)洛心,vs似乎始終按照自己支持的最高標(biāo)準(zhǔn)來(lái)執(zhí)行掉伏,不支持調(diào)節(jié)