Linux 下centos 查看 -std这个编译时命令 是否支持 C17

实际工作中,可能会遇到c++的一些高级特性,例如std::invoke,此函数是c++17才引入的,如何判断当前的gcc是否支持c++17呢,这里提供两种办法。
1.根据gcc的版本号来推断
gcc --version,可以查看版本号,笔者的电脑,gcc的版本号是8.3.0,然后查看8.3.0是什么时候发布的,查出是2019年发布的,从而推断出是支持,但是不是十分肯定,所以接着看第二种方法
2.直接man gcc
![[Pasted image 20230321110013.png]]

上图中,红色方框-std=表示支持的标准项,可以看到c11,c17之类的

centos 本身自带的版本是不支持到C17的。截取 man gcc如下

 -std=
           Determine the language standard.   This option is currently only supported when compiling C or
           C++.

           The compiler can accept several base standards, such as c90 or c++98, and GNU dialects of those
           standards, such as gnu90 or gnu++98.  When a base standard is specified, the compiler accepts all
           programs following that standard plus those using GNU extensions that do not contradict it.  For
           example, -std=c90 turns off certain features of GCC that are incompatible with ISO C90, such as
           the "asm" and "typeof" keywords, but not other GNU extensions that do not have a meaning in ISO
           C90, such as omitting the middle term of a "?:" expression. On the other hand, when a GNU dialect
           of a standard is specified, all features supported by the compiler are enabled, even when those
           features change the meaning of the base standard.  As a result, some strict-conforming programs
           may be rejected.  The particular standard is used by -Wpedantic to identify which features are
           GNU extensions given that version of the standard. For example -std=gnu90 -Wpedantic warns about
           C++ style // comments, while -std=gnu99 -Wpedantic does not.


            A value for this option must be provided; possible values are

           c90
           c89
           iso9899:1990
               Support all ISO C90 programs (certain GNU extensions that conflict with ISO C90 are
               disabled). Same as -ansi for C code.

           iso9899:199409
               ISO C90 as modified in amendment 1.

           c99
           c9x
           iso9899:1999
           iso9899:199x
               ISO C99.  Note that this standard is not yet fully supported; see
               <http://gcc.gnu.org/c99status.html> for more information.  The names c9x and iso9899:199x are
               deprecated.

           c11
           c1x
           iso9899:2011
               ISO C11, the 2011 revision of the ISO C standard.  Support is incomplete and experimental.
               The name c1x is deprecated.

           gnu90
           gnu89
               GNU dialect of ISO C90 (including some C99 features). This is the default for C code.

           gnu99
           gnu9x
               GNU dialect of ISO C99.  When ISO C99 is fully implemented in GCC, this will become the
               default.  The name gnu9x is deprecated.

           gnu11
           gnu1x
               GNU dialect of ISO C11.  Support is incomplete and experimental.  The name gnu1x is
               deprecated.

           c++98
           c++03
               The 1998 ISO C++ standard plus the 2003 technical corrigendum and some additional defect
               reports. Same as -ansi for C++ code.

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

           c++11
           c++0x
               The 2011 ISO C++ standard plus amendments.  Support for C++11 is still experimental, and may
               change in incompatible ways in future releases.  The name c++0x is deprecated.
 gnu++11
           gnu++0x
               GNU dialect of -std=c++11. Support for C++11 is still experimental, and may change in
               incompatible ways in future releases.  The name gnu++0x is deprecated.

           c++1y
               The next revision of the ISO C++ standard, tentatively planned for 2017.  Support is highly
               experimental, and will almost certainly change in incompatible ways in future releases.

           gnu++1y
               GNU dialect of -std=c++1y.  Support is highly experimental, and will almost certainly change
               in incompatible ways in future releases.



分享一个有趣的 学习链接:https://xxetb.xet.tech/s/HY8za

Linux中使用C++17标准来编译代码,你需要确保你的编译支持C++17,并且你的系统已经安装了相应的编译器。GCC(GNU Compiler Collection)和Clang都是支持C++17的编译器。以GCC为例,以下是一些步骤: 1. 更新你的系统和软件包管理器,确保你安装了最新的GCC版本。在基于Debian的系统(比如Ubuntu)上,你可以使用以下命令: ``` sudo apt update sudo apt upgrade sudo apt install g++ ``` 在基于Red Hat的系统(比如CentOS)上,你可以使用: ``` sudo yum update sudo yum install gcc-c++ ``` 2. 安装完编译器后,你可以使用`g++`命令编译你的C++代码。默认情况下,GCC可能不会使用C++17标准,你需要通过特定的编译选项来启用它。为了启用C++17标准,可以使用`-std=c++17`标志。例如,如果你有一个名为`main.cpp`的源文件,你可以这样编译它: ``` g++ -std=c++17 -o main main.cpp ``` 这条命令会将`main.cpp`编译成一个名为`main`的可执行文件。 3. 如果你想让GCC默认使用C++17标准进行编译,你可以在你的家目录下的`.bashrc`或`.bash_profile`文件中添加一个别名,这样每次使用`g++`都会默认使用C++17标准。例如: ``` alias g++='g++-7 -std=c++17' ``` 这样做后,你需要重新加载配置文件,比如通过运行`source ~/.bashrc`,或者重新打开一个终端。 4. 另外,确保你的代码是C++17兼容的,因为使用了新特性之后,代码必须符合C++17标准的规定。 5. 如果你有多个版本的GCC,你可能需要使用`g++-7`、`g++-8`等命令来指定使用哪个版本的编译器。 6. 如果你的系统中同安装了多个版本的GCC,你还可以使用`update-alternatives`命令来管理不同版本的GCC和G++编译器的默认版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值