在 Ubuntu 上学习 C++ 编程是非常常见且高效的选择。下面从环境准备 → 编写代码 → 编译运行 → 学习资源一步步说明。
sudo apt update
sudo apt upgrade
Ubuntu 自带或可通过 apt 安装:
sudo apt install g++
查看版本:
g++ --version
sudo apt install code
mkdir cpp_learn
cd cpp_learn
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++!
g++ 源文件.cpp -o 输出名
g++ -std=c++17 源文件.cpp -o 输出名 # 指定 C++ 标准
g++ -Wall 源文件.cpp -o 输出名 # 显示警告
cin / cout)gdb 调试:g++ -g test.cpp -o test
gdb ./test
如果你愿意,我可以:
你现在是完全新手,还是已经有一点基础?