在 Ubuntu 上安装 C++ 编译器,最常用的是 GCC(g++)。下面是几种常见、推荐的安装方式,按使用场景说明。
sudo apt update
sudo apt install g++
g++ --version
如果看到类似输出,说明安装成功:
g++ (Ubuntu 11.4.0) 11.4.0
如果你不只是编译 C++,还可能需要 make、gcc、g++ 等工具:
sudo apt install build-essential
包含内容:
gccg++make验证:
gcc --version
g++ --version
make --version
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++!
apt list gcc-* g++-*
sudo apt install gcc-12 g++-12
sudo update-alternatives --config g++
如果你更喜欢 Clang:
sudo apt install clang
使用方式:
clang++ hello.cpp -o hello
sudo apt update
sudo apt install g++
如果你告诉我:
我可以给你更精确的建议(比如 CMake、IDE、交叉编译等)。