如何检测c ++ 11支持带有cmake的编译器

如何检测c ++ 11支持带有cmake的编译器,第1张

直接测试新写法

[plain] view plain copy

<pre name="code" class="plain">#CMakeLists.txt

project(test)

cmake_minimum_required(VERSION 2.8)

aux_source_directory(. SRC_LIST)

add_executable(${PROJECT_NAME} ${SRC_LIST})

include(CheckCXXCompilerFlag)

CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)

CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)

if(COMPILER_SUPPORTS_CXX11)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

elseif(COMPILER_SUPPORTS_CXX0X)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

else()

message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")

endif()

测试c++11代码

[cpp] view plain copy

//test.cc

#include <iostream>

#include<vector>

using namespace std

int main()

{

const std::vector<int>v(1)

auto a = v[0]//a为int类型

cout <<"a : "<<a <<endl

decltype(v[0]) b = 0//b为const int&类型,即std::vector<int>::operator[](size_type)const的返回类型

auto c = 0//c为int类型

auto d = c//d为int类型

decltype(c) e//e为int类型,c实体的类型

decltype((c)) f = e//f为int&类型,因为(c)是左值

decltype(0) g//g为int类型,因为0是右值

return 0

}

所以,不同版本的gcc给指定c++11支持设定了不同的标志,也就说老版本支持-std=c++0x的写法,新版本用-std=c++11的写法。以上程序就是判断本机的g++该使用那种输出版本。

#include #include int main( ) { long start,endstart = clock()//测试的程序段 end = clock()printf("%ld\n",start-end)//单位:毫秒 return 0}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12168969.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存