在Windows上使用VSCode远程链接到Linux上开发并调试C++程序

Linux安装相应工具

Debian 和 Ubuntu

apt install -y gcc make gdb

CentOS

yum install -y gcc make gdb

VSCode安装Remote Development

1.jpg

安装后可以看到一个新的图标,点击后选中SSH Targets

2.jpg

添加链接配置

新建config文件

D:_Projects_Source_bak\2021\linux\ssh\config


3.jpg

编辑完config配置文件保存后,VSCode配置链接


4.jpg

配置完成后,在SSH TARGETS下会显示config配置的远程目标(主机)

链接

右键点击远程目标(主机),选择任意一种connect
如果出现如下错误

5.jpg

表示本地(windows)电脑没有安装SSH客户端,可以安装Git客户端解决
下载地址:https://www.git-scm.com/downloads
然后会提示输入远程Linux登录密码
6.jpg

链接成功,打开远程linux C++工程存放路径
7.jpg

会列出Linux的文件系统,让你选择工作目录,比如(新建: root\ProDir),选好后点击旁边的OK即可:
如果使用密码验证,此处有可能会让你再输入一次密码.后面很多步骤也有可能反复验证
8.jpg

Linux端安装C/C++扩展

注意,这些扩展需要VSCode链接到Linux后再安装,安装时会显示"安装到SSH:远程linux"
9.jpg

创建工程

10.jpg

main.c

#include

void main()
{
    int a=0;
    a++;
    a+=2;
    a-=3;
    printf("a=%d\n",a);
    return;
}

Makefile

# C compiler options
CC  = gcc
#CFLAGS = -g -O2             
RELEASE = release.elf
DEBUG   = debug.elf
LIBS = 
INC = 

# Source files
SRCS = main.c

# Make everything
all:    $(RELEASE) $(DEBUG)
 
# Make the application
$(RELEASE): $(OBJS)
    $(CC) -o $(RELEASE) $(SRCS) $(LIBS)

$(DEBUG): $(OBJS)
    $(CC) -o $(DEBUG) $(SRCS) $(LIBS) -ggdb3

#
# Clean all object files...
#
clean:
    $(RM) $(DEBUG) $(RELEASE) 

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    
    // "version": "0.2.0",
    // "configurations": [
    //     {
    //         "name": "(gdb) Launch",
    //         "type": "cppdbg",
    //         "request": "launch",
    //         "program": "enter program name, for example ${workspaceFolder}/a.out",
    //         "args": [],
    //         "stopAtEntry": false,
    //         "cwd": "${workspaceFolder}",
    //         "environment": [],
    //         "externalConsole": false,
    //         "MIMode": "gdb",
    //         "setupCommands": [
    //             {
    //                 "description": "Enable pretty-printing for gdb",
    //                 "text": "-enable-pretty-printing",
    //                 "ignoreFailures": true
    //             }
    //         ]
    //     }
    // ]

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/debug.elf",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "logging": {
                "moduleLoad": true,
                "engineLogging": true,
                "trace": true
            },
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
            {
                    "label": "make",
                    "command": "make",
                    "type": "process",
                    "args": [],
                    "problemMatcher": "$msCompile"
            }
    ]
}

开始调试

按F5,选择工程类型:


11.jpg

选择gcc版本:


12.jpg

可以看到VSCode成功进入了调试模式,左边还能显示所有变量的值:


13.jpg

如果以上过程有问题, 可关闭VSCode重启再试.

你可能感兴趣的:(在Windows上使用VSCode远程链接到Linux上开发并调试C++程序)