使用场景:C/C++代码在linux服务器上,在Windows上用VS Code可以调试这份代码。好处就是可以可视化。不然在linux用gdb,效率不高,也没那么好看。因人而异,看个人喜好吧。

一.  安装git(主要是为了能用ssh,省得每次都输入密码),官网:Git

1. 打开git bash,输入ssh-keygen -t rsa,一路回车,之后会在~/.ssh目录下生成生成id_rsa和id_rsa.pub文件;

2. 复制id_rsa.pub文件内容到linux下~/.ssh/authorized_keys,没目录就创建,没文件就创建;

3. 加权限,chmod 700 -R ~/.ssh

二. 安装并打开VS Code

1. 安装Remote Development插件;

2. 在设置里启用remote.SSH.showLoginTerminal

 3. ctrl+shift+p打开命令行,输入>remote ssh config会出现命令,选择,再选择C:\Users\xxx\.ssh\config,编辑:

# Read more about SSH config files: https://linux.die/man/5/ssh_config
Host Centos7.3 # 主机名
    HostName 192.168.88.200 # ip
    User root
    Port 22
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

点击连接,如果连接不上,看下ssh_config文件是否有错。等待安装vs code server。如果不能联网,得自己下载安装,参考文末。

三. 在ssh窗口安装C/C++ Extension Pack插件

注意:必须是连上ssh之后,在ssh窗口装这个插件。我看网上教程,都没说到这个问题,我都是安装好VS Code之后,就一下子把Remote Development和C/C++ Extension Pack插件装好,结果ssh连上之后,配置"type": "cppdbg",系统一直无法识别。重启电脑,重装软件,谷歌搜,都解决不了。两者区别就是,连上ssh后再装插件,插件是装在linux中的,可以看~/.vscode-server/extensions/是否有插件。

四. 调试nginx配置样例

创建并配置tasks.json(用于编译的,非必须):

{
    // See https://go.microsoft/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "make",
            "args": [
                "-j8",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /usr/bin/gcc"
        }
    ]
}

创建并配置launch.json(用于启动的,必须):

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

补充:

1. 离线安装vs code serverhttps://wwwblogs/sinicheveen/p/13812278.htmlhttps://wwwblogs/sinicheveen/p/13812278.html

2. 干净卸载VS Code

卸载软件之后还需删除以下两个目录

  • C:\Users\$用户名\.vscode
  • C:\Users\$用户名\AppData\Roaming\Code

注意:

1. 打开工作目录后,最好先打开目录下的任意文件,再新建tasks.json和launch.json,否则会出现莫名其妙的问题,我感觉这个VS Code的bug(还是我不会用?哈哈哈);

更多推荐

在Windows上用VS Code远程调试linux代码