Home > Development Tools > VSCode > body text

Detailed configuration explanation: remote debugging c++ in vscode

青灯夜游
Release: 2022-11-04 09:57:22
forward
3446 people have browsed it

Detailed configuration explanation: remote debugging c++ in vscode

Recently I am learning Linux webserver development and need to debug my own C/C code under Linux. However, Linux is not as convenient as directly Visio Studio or other integrated development environments under Windows. , it is quite troublesome to develop under Linux now. So you can consider using VScode for remote development. However, many tutorials on the Internet are not very clear. After trying many tutorials, I encountered many pitfalls. The final summary is as follows. [Recommended learning: "vscode tutorial"]

1. System configuration

Remote system: ubuntu18.04 (virtual machine)
Development host: windows10

2. Ubuntu remote installation software and settings

(1) Install necessary software: ssh (system communication), gdb, gsdbserver (code debugging):

sudo apt-get install openssh-server
sudo apt-get install gdb
sudo apt-get install gdbserver
Copy after login

(2) Create test folders and files

Note:

  • Although you may want to get it right in one step and test your final program directly, it is not recommended to do this here. It is recommended to create a new hello and world program to test first, and then test it after success. Debugging your own code.
  • The folder location and content don’t matter, but it’s best to keep it simple
cd ~/桌面
mkdir testvs
cd testvs
touch main.cpp
gedit main.cpp
Copy after login

The main.cpp code is:

#include <stdio.h>
 
int main()
{
    int a = 1;
    printf("hello world\n");
    getchar();
    return 0;
}</stdio.h>
Copy after login

(3 ) Compile and get the executable file

##g main.cpp -o main -gNote:

    Add -g option, otherwise you cannot use gdb to debug
  • After running, there are two files main.cpp and main in the testvs folder

(4) Start gdbserver

(4.1) First look at your ubuntu system ip address:

hostname -I
Detailed configuration explanation: remote debugging c++ in vscodeYou can get the local ip address as
192.168.199.131

(4.2) Start gdbserver (note changing the ip address and test file directory)

gdbserver 192.168.199.131:2000 ~/Desktop/testvs/main
Detailed configuration explanation: remote debugging c++ in vscode

##3. Host VScode settings

(1) First install the following plug-ins in VScode:

C/C
  • C/C Extension Pack
  • Remote - SSH
  • Remote Development

(2) ssh remote connectionLower left corner "Management"->"Control Panel", then find the option "Remote-SSH: Connect to Host..." -> Add New SSH Host...

Enter the ubuntu system IP address, and a new interface will appear


Enter the ubuntu system password in the red box. If the green IP address is displayed in the lower left corner, the connection is successful, as shown below. Detailed configuration explanation: remote debugging c++ in vscode

Detailed configuration explanation: remote debugging c++ in vscode

(3) Open the test file Open the folder-> Select the test folder directory , click the "OK" button

Select the C/C extension and "Install in SSH:XXX". The same applies to C/C Extension Pack Detailed configuration explanation: remote debugging c++ in vscode Then restart Vscode and gdbserver in Ubuntu (it must be restarted, otherwise an error will be reported in the next steps) and re-execute the above remote connection process.

(4) Set the configuration file

(4.1) Configure tasks.json

From Select Terminal>Configure Default Build Task in the menu bar, and select C/C in the drop-down bar: g build active file. Then generate the tasks.json file and replace the content with:

{
    // 有关 tasks.json 格式的文档,请参见
     // https://go.microsoft.com/fwlink/?LinkId=733558
     "version": "2.0.0",
     "tasks": [
     {
     "type": "shell",
     "label": "g++ build active file",
     "command": "/usr/bin/g++",
     "args": [
     "-std=c++11",
     "-g",
     "${file}",
     "-o",
     "${fileDirname}/${fileBasenameNoExtension}"
     ],
     "options": {
     "cwd": "/usr/bin"
     },
     "problemMatcher": [
     "$gcc"
     ],
     "group": {
     "kind": "build",
     "isDefault": true
     }
     },
     { //删除二进制文件
     "type": "shell",
     "label": "delete output file",
     "command": "rm",
     "args": [
     "${fileDirname}/${fileBasenameNoExtension}"
     ],
     "presentation": {
     "reveal": "silent", //删除过程不切换终端(专注程序输出)
     }
     }
     ]
    }
Copy after login

(4.2) configuration launch.json

Select Debug>Add Configuration in the menu bar, select C (GDB/LLDB), select g build and debug active file in the drop-down bar. Generate launch.json, and change the content to:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
    "name": "g++ build and debug active file",
    "type": "cppdbg",
    "request": "launch",
    "program": "${fileDirname}/${fileBasenameNoExtension}",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "setupCommands": [
    {
     "description": "为 gdb 启用整齐打印",
     "text": "-enable-pretty-printing",
     "ignoreFailures": true
    }
    ],
    "preLaunchTask": "g++ build active file",
    "postDebugTask": "delete output file",
    "miDebuggerPath": "/usr/bin/gdb"
    }
    ]
   }
Copy after login

4. Run debugging

Debug and run under main.cpp

Detailed configuration explanation: remote debugging c++ in vscodeMore about For VSCode related knowledge, please visit:

vscode Basic Tutorial

!

The above is the detailed content of Detailed configuration explanation: remote debugging c++ in vscode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template