使用场景:C/C++代码在linux服务器上,用在Windows上VS Code可以调试这份代码
一. 安装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:Usersxxx.sshconfig,编辑:
# Read more about SSH config files: https://linux.die.net/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.com/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": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/gcc"
}
]
}
创建并配置launch.json(用于启动的,必须):
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/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://www.cnblogs.com/sinicheveen/p/13812278.htmlhttps://www.cnblogs.com/sinicheveen/p/13812278.html
2. 干净卸载VS Code
卸载软件之后还需删除以下两个目录
C:Users$用户名.vscodeC:Users$用户名AppDataRoamingCode



