在 Ubuntu 上“设置 C++ 编译器”通常包含安装编译器、验证是否可用、以及配置开发环境三步。下面按最常见情况说明:
Ubuntu 默认通常没有安装 g++,需要先装:
sudo apt update
sudo apt install g++ -y
如果你还想装完整开发工具(推荐):
sudo apt install build-essential -y
build-essential 包含:
gcc(C 编译器)g++(C++ 编译器)makeg++ --version
如果看到类似:
g++ (Ubuntu 11.4.0) 11.4.0
说明安装成功。
nano hello.cpp
内容:
#include <iostream>
using namespace std;
int main() {
cout << "Hello Ubuntu C++" << endl;
return 0;
}
保存:Ctrl + O → 回车
退出:Ctrl + X
g++ hello.cpp -o hello
./hello
输出:
Hello Ubuntu C++
g++ -std=c++11 hello.cpp -o hello
g++ -std=c++17 hello.cpp -o hello
安装:
sudo apt install cmake -y
示例 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(demo)
add_executable(demo main.cpp)
编译:
mkdir build && cd build
cmake ..
make
sudo apt install clang -y
使用:
clang++ hello.cpp -o hello
如果你是想:
可以告诉我你的具体用途,我可以直接给你对应方案。