在 CentOS 中查看 C++ 编译器(通常是 g++)的版本,以及系统/项目中使用的 C++ 标准版本,有几种常见方式:
g++ --version
或
g++ -v
如果提示 g++: command not found,说明未安装:
sudo yum install gcc-c++
gcc --version
echo | g++ -dM -E -x c++ - | grep __cplusplus
或更直观一点:
g++ -std=c++17 -dM -E -x c++ - | grep __cplusplus
(换 c++11 / c++14 / c++20 可测试支持情况)
在 C++ 程序中打印:
#include <iostream>
int main() {
std::cout << __cplusplus << std::endl;
}
201103L → C++11201402L → C++14201703L → C++17202002L → C++20编译时指定标准:
g++ test.cpp -std=c++17
alternatives --list | grep c++
或
which g++
clang++ --version
如果你是想确认 CentOS 自带的 C++ 版本是否过低(例如 CentOS 7 默认 g++ 4.8.5,仅支持 C++11),也可以告诉我你的 CentOS 版本,我可以给你升级建议(如 devtoolset / SCL)。