Windows平台 CLion 远程调试 Linux 的 C++ 程序
Windows平台 CLion 远程调试 Linux 的 C++ 程序
1. CLion 的安装
Pass
2. Linux 环境的配置
2.1. 安装 gdbserver
这里举例 Ubuntu 环境下的安装:
sudo apt-get install gdbserver
2.2 配置CLion
2.2.1. 配置 Toolchains
首先在CLion 的 File -> Settings -> Tools -> SSH Configurations
中添加一个 SSH 配置,配置好用户名、密码、IP地址、端口号等信息。
点击 File -> Settings -> Build, Execution, Deployment -> Toolchains
,点击 + 号,添加一个新的 Toolchains:Remote Host。
其中 Credentials 选择刚才配置好的 SSH 配置,CMake 选择 Remote Host CMake,Debugger 选择 Remote Host GDB。
然后点击 File -> Settings -> Build, Execution, Deployment -> CMake
,将 Toolchains 选择刚才配置好的 Toolchains。
然后点击 File -> Settings -> Build, Execution, Deployment -> Deployment
,(注意这里如果添加,添加的是SFTP,即Connection的Type是SFTP)将Connection 的 SSH Configurations 选择刚才配置好的 SSH 配置。将Mappings 中的 Deployment path 选择你的 Linux 环境中的目录,比如 /home/username/CLionProjects/
。Local path 选择你的 Windows 环境中的目录,比如 D/CLionProjects/
。
2.2.2. 配置 CMakeLists.txt
Pass
2.2.3. 配置 Debug
点击 Run -> Edit Configurations
,点击 + 号,选择 GDB Remote Debug。
在 Debugger 选项中,选择刚才配置好的 Toolchains。
'target remote' args 选项中,填写你的 Linux 环境中的 IP 地址和端口号,格式:txp:IP:端口号 比如:tpc:xxx.xxx.xxx.xxx:1234。
在 Path mappings 选项中,设置映射目录,将 Deployment path 选择你的 Linux 环境中的目录,比如 /home/username/CLionProjects/
。Local path 选择你的 Windows 环境中的目录,比如 D:/CLionProjects/
。
3. 远程调试
3.1. Linux 环境下的配置
首先在 Linux 环境中,进入你的项目目录,比如 /home/username/CLionProjects/
,然后执行命令:
# 创建build目录
touch build
# 进入build目录
cd build
# 生成Makefile
cmake .. -DCMAKE_BUILD_TYPE=Debug
# 编译
make
# 运行gdbserver在1234端口上进行调试
gdbserver :1234 ./your_executable_file
其中 1234 是端口号,与CLion里配置debug的端口号一致,your_executable_file 是你的可执行文件。
输入gdbserver :1234 ./your_executable_file后,会有如下提示:
Process your_executable_file created; pid = xxxx
Listening on port 1234
然后在 CLion 中,点击 Run -> Debug 'your_executable_file',就可以开始调试了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)