在 Ubuntu 上测试(编译、运行、调试)C++ 代码,一般按下面几步来:
Ubuntu 默认可能没有 g++,先安装:
sudo apt update
sudo apt install g++ build-essential
检查是否安装成功:
g++ --version
创建文件 test.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello Ubuntu C++" << endl;
return 0;
}
g++ test.cpp -o test
g++ -g test.cpp -o test
g++ -Wall -Wextra -g test.cpp -o test
./test
输出:
Hello Ubuntu C++
gdb ./test
常用 gdb 命令:
run 运行break main 设断点next 单步print 变量名 查看变量quit 退出g++ main.cpp func.cpp -o app
或写一个简单的 Makefile:
app: main.cpp func.cpp
g++ -g -Wall main.cpp func.cpp -o app
使用:
make
./app
g++ -x c++ - <<'EOF'
#include <iostream>
int main(){ std::cout << "test\n"; }
EOF
./a.out
gdb:调试valgrind:内存检查clang++:替代编译器vscode / vim:编辑器如果你有具体代码或想测试某类程序(算法 / 多线程 / 网络),可以发出来,我可以直接帮你跑和改。