在 Ubuntu 上写 C++ 程序通常分为 安装环境 → 编写代码 → 编译运行 几个步骤。下面给你一个从零开始的清晰流程。
sudo apt update
sudo apt install g++ 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++!
sudo apt install code
或官网下载 .deb 安装。
安装 C++ 插件:
g++ main.cpp func.cpp -o app
或使用 Makefile / CMake 管理项目。
如果运行报错:
bash: ./hello: Permission denied
执行:
chmod +x hello
确保文件编码为 UTF-8,终端也使用 UTF-8。
如果你愿意,我可以:
你现在是用 纯命令行 还是想用 图形界面编辑器?