The note is generated by ChatGPT.
1. Install C++ Environment
在 Linux 或 WSL 里,C/C++ 程序需要编译器。常用的是 g++(GNU C++ compiler)。
同时,调试程序需要 gdb(GNU Debugger)。
运行:
| |
检查是否安装成功:
| |
2. Normal Way to Compile and Run C++
这是最常见、最基本的用法:
写代码
hello.cpp1 2 3 4 5 6 7#include <iostream> using namespace std; int main() { cout << "Hello from Linux C++!" << endl; return 0; }编译
1g++ hello.cpp -o hello👉 把
hello.cpp编译成可执行文件hello运行
1./hello👉 输出:
Hello from Linux C++!
⚠️ 注意:和 Python 的
python hello.py不同,C++ 必须先编译再运行。
3. Create cprun Helper Function
C++ 每次都要先编译再运行,比较麻烦。
为了方便,我们写一个 shell 函数 cpprun,让你一条命令就能完成编译+运行。
在 ~/.zshrc 或 ~/.bashrc 里加上以下内容:
| |
让配置生效:
| |
4. Run with cpprun
直接运行
1cpprun hello.cpp传递参数
1cpprun hello.cpp arg1 arg2调试模式
1cpprun hello.cpp --debug