在 Ubuntu 上,**“C++ 版本”**通常有两种含义,下面分别说明如何查看。
g++ --version
示例输出:
g++ (Ubuntu 11.4.0) 11.4.0
这表示使用的是 GCC 11.4.0,支持到 C++20(部分 C++23)。
gcc --version
g++ -std=c++17 -E -x c++ /dev/null
或更直观地:
echo | g++ -dM -E -x c++ - | grep cplusplus
输出示例:
#define __cplusplus 201703L
含义:
199711L → C++98201103L → C++11201402L → C++14201703L → C++17202002L → C++20g++ --help=common | grep std=
Ubuntu 可能安装了多个版本:
ls /usr/bin/g++*
切换默认版本:
sudo update-alternatives --config g++
在 C++ 程序中:
#include <iostream>
int main() {
std::cout << __cplusplus << std::endl;
}
编译并运行:
g++ test.cpp -std=c++17
./a.out
| Ubuntu 版本 | 默认 GCC |
|---|---|
| 20.04 | GCC 9 |
| 22.04 | GCC 11 |
| 24.04 | GCC 13 |
如果你是想确认某个项目用的是哪个 C++ 标准(如 C++11 / C++17),可以告诉我你的构建方式(Makefile / CMake / vscode),我可以具体帮你查。