search
HomeDevelopment ToolsVSCodeDetailed configuration explanation: remote debugging c++ in vscode

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

(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

The main.cpp code is:

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

(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", //删除过程不切换终端(专注程序输出)
     }
     }
     ]
    }

(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"
    }
    ]
   }

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!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Visual Studio and VS Code: Understanding Their Key DifferencesVisual Studio and VS Code: Understanding Their Key DifferencesApr 19, 2025 am 12:16 AM

VisualStudio is suitable for large-scale projects and enterprise-level application development, while VSCode is suitable for rapid development and multilingual support. 1. VisualStudio provides a comprehensive IDE environment and supports Microsoft technology stack. 2.VSCode is a lightweight editor that emphasizes flexibility and scalability, and supports cross-platform.

Is Visual Studio Still Free? Understanding the AvailabilityIs Visual Studio Still Free? Understanding the AvailabilityApr 18, 2025 am 12:05 AM

Yes, some versions of VisualStudio are free. Specifically, VisualStudioCommunityEdition is free for individual developers, open source projects, academic research, and small organizations. However, there are also paid versions such as VisualStudioProfessional and Enterprise, suitable for large teams and enterprises, providing additional features.

Using Visual Studio: Developing Software Across PlatformsUsing Visual Studio: Developing Software Across PlatformsApr 17, 2025 am 12:13 AM

Cross-platform development with VisualStudio is feasible, and by supporting frameworks like .NETCore and Xamarin, developers can write code at once and run on multiple operating systems. 1) Create .NETCore projects and use their cross-platform capabilities, 2) Use Xamarin for mobile application development, 3) Use asynchronous programming and code reuse to optimize performance to ensure efficient operation and maintainability of applications.

How to format json with vscodeHow to format json with vscodeApr 16, 2025 am 07:54 AM

The ways to format JSON in VS Code are: 1. Use shortcut keys (Windows/Linux: Ctrl Shift I; macOS: Cmd Shift I); 2. Go through the menu ("Edit" > "Format Document"); 3. Install JSON formatter extensions (such as Prettier); 4. Format manually (use shortcut keys to indent/extract blocks or add braces and semicolons); 5. Use external tools (such as JSONLint and JSON Formatter).

How to compile vscodeHow to compile vscodeApr 16, 2025 am 07:51 AM

Compiling code in VSCode is divided into 5 steps: Install the C extension; create the "main.cpp" file in the project folder; configure the compiler (such as MinGW); compile the code with the shortcut key ("Ctrl Shift B") or the "Build" button; run the compiled program with the shortcut key ("F5") or the "Run" button.

How to install vscodeHow to install vscodeApr 16, 2025 am 07:48 AM

To install Visual Studio Code, please follow the following steps: Visit the official website https://code.visualstudio.com/; download the installer according to the operating system; run the installer; accept the license agreement and select the installation path; VSCode will start automatically after the installation is completed.

How to enlarge fonts with vscodeHow to enlarge fonts with vscodeApr 16, 2025 am 07:45 AM

The methods to enlarge fonts in Visual Studio Code are: open the settings panel (Ctrl, or Cmd,). Search and adjust "Font Size". Choose "Font Family" with the right size. Install or select a theme that provides the right size. Use keyboard shortcuts (Ctrl or Cmd) to enlarge the font.

How to connect to a remote server with vscodeHow to connect to a remote server with vscodeApr 16, 2025 am 07:42 AM

How to connect to a remote server through VSCode? Install Remote - SSH Extended Configuration SSH Create a Connection in VSCode Enter connection information: Host, Username, Port, SSH Key Double-click the saved connection in Remote Explorer

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.