在 Ubuntu 上安装 C++ 编译器(通常是 g++)非常简单,步骤如下:
更新软件包列表
sudo apt update
安装 build-essential(包含 g++、gcc、make 等)
sudo apt install build-essential
验证安装
g++ --version
如果输出版本号(如 g++ (Ubuntu 11.4.0...)),说明安装成功。
如果你不想装整个 build-essential,也可以只装 g++:
sudo apt update
sudo apt install g++
创建测试文件:
nano hello.cpp
写入内容:
#include <iostream>
int main() {
std::cout << "Hello, Ubuntu!" << std::endl;
return 0;
}
编译并运行:
g++ hello.cpp -o hello
./hello
输出:
Hello, Ubuntu!
sudo apt install clang
使用方式:
clang++ hello.cpp -o hello
如果你是在 WSL / 服务器 / 树莓派 上装,步骤也基本一样。需要我帮你配 VS Code 或 CMake 吗?