首页 > 编程语言 >通过__cplusplus查看c++版本并检查gcc支持版本

通过__cplusplus查看c++版本并检查gcc支持版本

时间:2024-04-13 13:55:05浏览次数:20  
标签:__ gcc enable 20 -- c++ C++ 版本 test

虽然平时开发都是用的c++11标准,但打算看一下wsl ubuntu里面的gcc默认支持什么标准~

1 打印__cplusplus宏

#include <cstdio>
int main() {
  printf("%ld\n", __cplusplus);
}
➜  test g++ test_cpp_version.cpp -o test_cpp_version           
➜  test ./test_cpp_version
201402

❓__cplusplus宏的作用:

  1. 在代码中区分当前的编译环境是 C 还是 C++。(通常在C++代码中嵌入C代码时使用)
  2. 查看当前C++版本。

2 指定C++版本编译

通过-std=c++17来指定:

➜  test g++ test_cpp_version.cpp -o test_cpp_version -std=c++17
➜  test ./test_cpp_version
201703

3 查看GCC版本

➜  test g++ -v      
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.2' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-9QDOt0/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) 

4 GCC9能否支持c++20

按网络资料显示,gcc9可以支持到C++20,进行一波测试:

4.1 通过命令行来检查是否支持

➜  test g++ -std=c++2a -E - < /dev/null
cc1: warning: command line option ‘-std=c++2a’ is valid for C++/ObjC++ but not for C
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "<stdin>"

这种输出表明是该GCC版本是有支持C++20的(尽管需要指定为C++2a)。

❓该命令的解释:

  1. -std=c++2a:指定编译器应使用的 C++ 语言标准。c++2a 是 C++20 标准的早期代号,表示使用 C++20 的特性集进行编译。
  2. -E:编译器选项,告诉 g++ 只执行预处理操作。
  3. -:表示 g++ 应该从标准输入读取源代码而不是从文件中读取。在这个命令中,它用来指示编译器从接下来的输入中获取要处理的代码。
  4. < /dev/null:使用 shell 的输入重定向功能。/dev/null 是一个特殊的设备文件,通常被称为“空设备”或“黑洞”。向 /dev/null 写入的数据会被丢弃,从 /dev/null 读取的内容则什么也没有。这里,它被用来作为 g++ 的输入源,意味着编译器实际上没有任何数据要处理。

如果没有支持,会这样显示:

➜  test g++ -std=c++23 -E - < /dev/null
g++: error: unrecognized command line option ‘-std=c++23’; did you mean ‘-std=c++03’?

4.2 打印__cplusplus宏

➜  test g++ test_cpp_version.cpp -o test_cpp_version -std=c++2a
➜  test ./test_cpp_version
201709

这里打印出来其实是201709,据说原因如下:

对于 GCC 9.4.0,尽管该版本支持大部分 C++20 的新特性(在 -std=c++2a-std=c++20 模式下),但标准正式发布的宏定义(202002L)在这个版本中尚未实现。GCC 9.4.0 中的 C++20 支持主要是基于 C++20 标准的较早草案,而不是最终版本。因此,它使用了一个较早的日期值“201709L”。

参考资料

GCC各版本对C++的支持情况

linux如何查看编译器支持的C++版本(支持C++11、支持C++14、支持C++17、支持C++20)(编译时不指定g++版本,默认使用老版本编译)

How do I check for C++20 support? What is the value of __cplusplus for C++20?

标签:__,gcc,enable,20,--,c++,C++,版本,test
From: https://www.cnblogs.com/rthete/p/18132779

相关文章