下面我按从零开始的流程,给你讲新手如何在 Ubuntu 上搭建 C++ 开发环境,包括安装系统、安装编译器、写第一个程序、以及常见工具推荐。
在 Windows 终端(管理员) 执行:
wsl --install -d Ubuntu
安装完成后,打开 Ubuntu 终端即可。
Ubuntu 自带 gcc,但 C++ 需要 g++。
sudo apt update
sudo apt install build-essential
g++ --version
能看到版本号就说明成功了。
mkdir cpp_demo
cd cpp_demo
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++!
nano / vimsudo snap install code --classic
安装 C++ 插件:
command not found说明没装 g++,重新执行:
sudo apt install g++
先学这几个就够:
ls # 查看文件
cd # 进入目录
pwd # 当前路径
rm # 删除
.h / .cpp)如果你愿意,我可以:
你现在是 纯 Ubuntu / WSL / 虚拟机 哪一种?