在 Ubuntu 上安装 C++ 的 GCC(g++)编译器,可以按下面步骤来:
sudo apt update
sudo apt install build-essential
这个包会一次性安装:
gcc(C 编译器)g++(C++ 编译器)makesudo apt install g++
g++ --version
输出类似:
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
说明安装成功 ✅
nano hello.cpp
内容:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, Ubuntu C++!" << endl;
return 0;
}
g++ hello.cpp -o hello
./hello
输出:
Hello, Ubuntu C++!
sudo apt install g++
sudo apt install g++-12
切换默认版本:
sudo update-alternatives --config g++
如果你用的是 Ubuntu 版本(18.04 / 20.04 / 22.04 / 24.04) 或 WSL / Docker,可以告诉我,我可以给你更精确的安装方式。