Windows平台 CLion 远程调试 Linux 的 C++ 程序

发布时间 2023-09-09 15:06:46作者: BryceAi

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',就可以开始调试了。