VSCode连接远程linux服务器进行C++开发
1. 环境
· 1.52.1 (user setup)
· Win10
· 远程linux服务器
2. 安装VSCode
推荐下载64bit User Installer,当然其它也可以。
3. 安装VSCode插件Remote – SSH
这个插件是连接远程服务器开发的核心插件,必须有。
可以在线安装,也可以离线安装,离线安装方法参考:VSCode搭建Python开发环境
4. 生成秘钥并配置
4.1 生成秘钥
Win图标 > 右单机 > Windows PowerShell (管理员)(A) > 输入: ssh-keygen.exe -t rsa > 一路狂按Enter > 复制生成的id_rsa路径到记事本备用(路劲格式为:C:\Users\zWX992246/.ssh/id_rsa,对你没看错,既有正斜杠又有反斜杠,不要怀疑) > 记事本打开生成的id_rsa.pub文件等待后续使用。
笔者这里已经生成过id_rsa了,所以显示的信息不一样。如果你以前生成过id_rsa了,那么就不用再次生成了。这个id_rsa在本机VSCode连接远程服务器时认证使用。
4.2 配置VSCode和远程服务器的认证秘钥
1、安装完Remote – SSH后,VSCode左侧栏会有「远程资源管理器」图标,如果没有就右单机打开
2、打开Remote -SSH配置文件
配置完毕之后,「远程资源管理器」中会显示你已经配置的服务器:
3、打开远程linux服务器添加认证秘钥
把刚才打开的id_rea.pub里边的内容追加到authorized_keys里边:
5. 在VSCode中打开远程服务器
在「远程资源管理器」选择一个服务器进行连接:
初次连接问题:
· 初次连接会提示你选择远程服务器平台,这里我们例子中的服务器都是linux,选linux就OK
· 初次连接可能会有一些其它提示框,看着选就完事了
· 初次连接会在远程linux服务器上下载vscode server,本机的VSCode和远程linux服务器正是通过vscode server进行交互的,所以这个vscode server是必须有的。但是对公司环境来说,vscode server可能会自动下载安装失败,此时就需要手动在服务器端安装vscode server,vscode server离线安装教程参看:离线安装 vs code server
· 安装好的vscoder server在用户根目录下:
正确连接之后,VSCode左下角会显示服务器信息:
6. 安装C++开发插件C/C++
你以为安装这个插件就完事了?安装这个插件的时候还会自动下载cpptools并安装(cpptools是必须的,C/C++扩展依赖这个插件),但公司环境,cpptools会下载安装失败,此时还需要自己手动离线安装,vscode会提示你cpptools下载失败,让你去https://github.com/microsoft/vscode-cpptools/releases自己下载安装。
打开https://github.com/microsoft/vscode-cpptools/releases,可以看到cpptools有很多平台的版本,我们需要确定远程linux服务器的架构,按照需求下载对应版本。
打开远程linux服务器,输入:uname -a查看linux架构等信息:
可以看到我们的服务器是x86架构的,所有这里就下载cpptools-linux.vsix,上传到linux服务器,打开vscode进行安装。不知道怎么安装离线的vsix插件?参看:VSCode搭建Python开发环境
至此,我们的C++开发环境就搭建完毕了,但是这还不是最优的配置,达不到最好的体验。
7. 优化配置
7.1 安装如下插件
Chinese (Simplified) Language Pack for Visual Studio Code
简介:Language pack extension for Chinese (Simplified)
简介:Mark lines and jump to them
简介:Improve your code commenting by annotating with alert, informational, TODOs, and more!
简介:A customizable extension for colorizing matching brackets
简介:支持多种语言的代码的立即执行。支持的语言:C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core), C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript, Elixir, Visual Basic .NET, Clojure, Haxe, Objective-C, Rust, Racket, AutoHotkey, AutoIt, Kotlin, Dart, Free Pascal, Haskell, Nim, D。
简介:CMake 语法补全,语法高亮。
简介:CMake 工程构建工具。
这些插件的好处,自己打开网址查看。
7.2 调整VSCode设置
配置本机(Win10)用户级setting.json:
VSCode > 文件 > 首选项 > 设置 > 用户 > 点击右上角一个图标(见下图) > 打开配置文件的json文件。
这里附上我的配置,个人认为还是比较优的,你也根据自己的需求调整:
{ “[cpp]”: { “editor.defaultFormatter”: “ms-vscode.cpptools” }, “editor.renderWhitespace”: “all”, “editor.codeActionsOnSave”: {}, “editor.dragAndDrop”: false, “editor.gotoLocation.alternativeDeclarationCommand”: “”, “editor.gotoLocation.alternativeDefinitionCommand”: “”, “editor.gotoLocation.alternativeTypeDefinitionCommand”: “”, “editor.padding.bottom”: 10, “editor.padding.top”: 10, “editor.renderLineHighlight”: “all”, “editor.roundedSelection”: false, “editor.formatOnPaste”: true, “editor.formatOnSave”: true, “editor.formatOnType”: true, “editor.suggest.shareSuggestSelections”: true, “editor.renderFinalNewline”: false, “editor.minimap.enabled”: false, “files.eol”: “\r\n”, “files.trimFinalNewlines”: true, “files.trimTrailingWhitespace”: true, “files.insertFinalNewline”: true, “files.exclude”: { “**/.repo”: true }, “files.watcherExclude”: { “**/.repo/**”: true, “**/.repo/*/**”: true, “**/.vscode/**”: true }, “workbench.activityBar.iconClickBehavior”: “focus”, “workbench.editor.mouseBackForwardToNavigate”: false, “workbench.tree.indent”: 24, “workbench.colorCustomizations”: { “editor.selectionBackground”: “#008000”, “editor.selectionHighlightBackground”: “#008000” }, “window.enableMenuBarMnemonics”: false, “window.customMenuBarAltFocus”: false, “search.searchEditor.reusePriorSearchConfiguration”: true, “search.showLineNumbers”: true, “search.useIgnoreFiles”: false, “terminal.integrated.copyOnSelection”: true, “bracketPairColorizer.forceUniqueOpeningColor”: false, “bracketPairColorizer.forceIterationColorCycle”: false, “explorer.confirmDelete”: false, “git.ignoreMissingGitWarning”: true, “pasteImage.encodePath”: “none”, “pasteImage.path”: “${currentFileDir}/imgs”, “pasteImage.defaultName”: “YMMDD_HHmmss”, “pasteImage.prefix”: “./”, “pasteImage.insertPattern”: “”, “code-runner.executorMap”: { “python”: “python”, }, “code-runner.clearPreviousOutput”: true, “code-runner.ignoreSelection”: true, “code-runner.runInTerminal”: true, “python.pythonPath”: “C:\\Program Files\\Python39\\python.exe”, “python.formatting.provider”: “yapf”, “python.linting.enabled”: true, “python.linting.flake8Enabled”: true, “python.linting.pylintEnabled”: false, “python.linting.flake8Args”: [ “–max-line-length=248”, “–extend-ignore = F403,F405” ], “better-comments.tags”: [ { “tag”: “#TODO”, “color”: “#FF8C00”, “strikethrough”: false, “underline”: false, “backgroundColor”: “transparent”, “bold”: false, “italic”: false }, { “tag”: “#DEBUG”, “color”: “#FF8C00”, “strikethrough”: false, “underline”: false, “backgroundColor”: “transparent”, “bold”: false, “italic”: false }, { “tag”: “#DEL”, “color”: “#FF2D00”, “strikethrough”: false, “underline”: false, “backgroundColor”: “transparent”, “bold”: false, “italic”: false }, { “tag”: “#QUE”, “color”: “#FF69B4”, “strikethrough”: false, “underline”: false, “backgroundColor”: “transparent”, “bold”: false, “italic”: false }, { “tag”: “#CAU”, “color”: “#ADFF2F”, “strikethrough”: false, “underline”: false, “backgroundColor”: “transparent”, “bold”: false, “italic”: false }, { “tag”: “#OPT”, “color”: “#32CD32”, “strikethrough”: false, “underline”: false, “backgroundColor”: “transparent”, “bold”: false, “italic”: false } ], “remote.SSH.remotePlatform”: { “10.175.118.187(log)”: “linux”, “10.175.82.130(精度)”: “linux” }, “cmake.configureOnOpen”: false, “python.showStartPage”: false } |
部分配置说明:
配置远程服务器的setting.json:
这里附上我的配置,个人认为还是比较优的,你也根据自己的需求调整:
{ “code-runner.executorMap”: { “cpp”: “cd $dir && rm -rf bin && mkdir bin > /dev/null 2>&1 && src=`find . -path ./build -prune -o -print | grep .cpp` && g++ $src -std=c++17 -o `pwd`/bin/$fileNameWithoutExt && `pwd`/bin/$fileNameWithoutExt”, “python”: “python” }, “code-runner.clearPreviousOutput”: true, “python.pythonPath”: “/usr/bin/python3.9”, “python.formatting.provider”: “yapf”, “python.linting.enabled”: true, “python.linting.flake8Enabled”: true, “python.linting.pylintEnabled”: false, “python.linting.flake8Args”: [ “–max-line-length=248”, “–extend-ignore = F403,F405” ], “C_Cpp.clang_format_path”: “/usr/bin/clang-format-10”, “C_Cpp.default.cppStandard”: “c++17”, “C_Cpp.default.cStandard”: “c11”, “C_Cpp.inactiveRegionOpacity”: 0.7, “C_Cpp.default.intelliSenseMode”: “gcc-x86”, “C_Cpp.intelliSenseEngineFallback”: “Enabled”, “C_Cpp.default.configurationProvider”: “ms-vscode.cmake-tools”, “clang-format.executable”: “/usr/bin/clang-format-10”, } |
部分配置说明:
8. 题外话
环境搭建好之后,相关文件在远程linux服务器的用户根目录下:
如果换了环境,又懒得重新安装vscode server和插件,那么我们只需打包.vscode-server文件,然后拷贝到另一台服务器解压到用户根目录下就OK了。
需要一个特别注意的问题:服务器的vscode server和本机的VSCode版本需要对应,若本机的VSCode更新了,那么你就连不上了,需要更新服务器的vscode server。
因为.vscode_server在根目录下放着,所有可以一次配置一个组的人都用,但是要保证VSCode版本一样。