下面给你一套在 CentOS 上配置 C++ 调试环境的常见、实用方案,从编译器 → 调试器 → 编辑器/IDE一步步来,适合新手和服务器环境。
sudo yum install -y gcc gcc-c++
验证:
g++ --version
sudo yum install -y gdb
验证:
gdb --version
sudo yum groupinstall -y "Development Tools"
包含:
test.cpp#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
int c = a + b;
cout << "c = " << c << endl;
return 0;
}
-g)g++ -g test.cpp -o test
✅ -g:生成调试信息(否则 GDB 看不到源码)
gdb ./test
| 命令 | 作用 |
|---|---|
run / r |
运行程序 |
break main |
在 main 设断点 |
break test.cpp:6 |
在指定行设断点 |
next / n |
单步(不进入函数) |
step / s |
单步(进入函数) |
print a |
打印变量 |
info locals |
查看局部变量 |
continue / c |
继续执行 |
quit / q |
退出 |
(gdb) break main
(gdb) run
(gdb) next
(gdb) print a
(gdb) continue
MakefileCXX = g++
CXXFLAGS = -g -Wall
test: test.cpp
$(CXX) $(CXXFLAGS) test.cpp -o test
clean:
rm -f test
使用:
make
gdb ./test
sudo yum install -y cgdb
cgdb ./test
Remote SSH → 连接 CentOS
.vscode/launch.json{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing"
}
]
}
]
}
✅ 支持:
g++ -g test.cpp -o test
升级 gcc(示例):
sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-11
scl enable devtoolset-11 bash
sudo yum install -y gcc gcc-c++ gdb
g++ -g test.cpp -o test
gdb ./test
如果你告诉我:
我可以给你更精确的配置方案。