在CentOS上配置C++的远程调试,通常需要使用GDB(GNU调试器)和SSH。以下是详细的步骤:
首先,确保你的CentOS系统上已经安装了GDB和SSH。
sudo yum install gdb openssh-server
为了方便远程调试,你可以配置SSH无密码登录。这需要生成SSH密钥对并将公钥复制到远程服务器。
ssh-keygen -t rsa
按照提示操作,通常会生成~/.ssh/id_rsa
(私钥)和~/.ssh/id_rsa.pub
(公钥)。
ssh-copy-id user@remote_host
其中,user
是远程服务器的用户名,remote_host
是远程服务器的IP地址或域名。
在远程服务器上启动GDB服务器。假设你的C++程序名为my_program
,并且你希望在远程服务器上调试它。
gdbserver :1234 /path/to/my_program
这里的:1234
是GDB服务器监听的端口,/path/to/my_program
是你的程序路径。
在本地机器上启动GDB客户端,并连接到远程服务器上的GDB服务器。
gdb -ex "target remote remote_host:1234" -ex "file /path/to/my_program" ./my_program
这里的remote_host
是远程服务器的IP地址或域名,1234
是GDB服务器监听的端口,/path/to/my_program
是你的程序路径。
现在,你可以在本地机器上的GDB客户端中进行调试。例如,设置断点、单步执行等。
break main
run
确保远程服务器上的防火墙允许GDB服务器监听的端口(例如1234)。
sudo firewall-cmd --zone=public --add-port=1234/tcp --permanent
sudo firewall-cmd --reload
如果你使用VSCode进行开发,可以配置VSCode进行远程调试。
F1
键,选择Remote-SSH: Connect to Host...
,输入远程服务器的IP地址和用户名。.vscode/launch.json
文件,配置调试设置。例如:{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Remote Debug",
"type": "cppdbg",
"request": "attach",
"program": "/path/to/my_program",
"processId": "${command:PickRemoteProcess}",
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"remoteMachine": {
"hostName": "remote_host",
"port": 1234,
"user": "user"
}
}
]
}
C++ Remote Debug
配置,然后按F5
键开始调试。通过以上步骤,你可以在CentOS上配置C++的远程调试。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:C++在CentOS上如何调试配置