VSCode連接遠程linux服務器進行C++開發


1. 環境

· 1.52.1 (user setup)

· Win10

· 遠程linux服務器

2. 安裝VSCode

clip_image001

推薦下載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文件等待后續使用。

clip_image002

筆者這里已經生成過id_rsa了,所以顯示的信息不一樣。如果你以前生成過id_rsa了,那么就不用再次生成了。這個id_rsa在本機VSCode連接遠程服務器時認證使用。

4.2 配置VSCode和遠程服務器的認證秘鑰

1、安裝完Remote - SSH后,VSCode左側欄會有「遠程資源管理器」圖標,如果沒有就右單機打開

clip_image003

2、打開Remote -SSH配置文件

clip_image005

clip_image007

配置完畢之后,「遠程資源管理器」中會顯示你已經配置的服務器:

clip_image008

3、打開遠程linux服務器添加認證秘鑰

clip_image009

把剛才打開的id_rea.pub里邊的內容追加到authorized_keys里邊:

clip_image011

5. 在VSCode中打開遠程服務器

在「遠程資源管理器」選擇一個服務器進行連接:

clip_image012

初次連接問題:

· 初次連接會提示你選擇遠程服務器平台,這里我們例子中的服務器都是linux,選linux就OK

· 初次連接可能會有一些其它提示框,看着選就完事了

· 初次連接會在遠程linux服務器上下載vscode server,本機的VSCode和遠程linux服務器正是通過vscode server進行交互的,所以這個vscode server是必須有的。但是對公司環境來說,vscode server可能會自動下載安裝失敗,此時就需要手動在服務器端安裝vscode server,vscode server離線安裝教程參看:離線安裝 vs code server

· 安裝好的vscoder server在用戶根目錄下:

· clip_image014

正確連接之后,VSCode左下角會顯示服務器信息:

clip_image015

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架構等信息:

clip_image017

可以看到我們的服務器是x86架構的,所有這里就下載cpptools-linux.vsix,上傳到linux服務器,打開vscode進行安裝。不知道怎么安裝離線的vsix插件?參看:VSCode搭建Python開發環境

clip_image019

至此,我們的C++開發環境就搭建完畢了,但是這還不是最優的配置,達不到最好的體驗。

7. 優化配置

7.1 安裝如下插件

Chinese (Simplified) Language Pack for Visual Studio Code

簡介:Language pack extension for Chinese (Simplified)

Bookmarks

簡介:Mark lines and jump to them

Better Comments

簡介:Improve your code commenting by annotating with alert, informational, TODOs, and more!

Bracket Pair Colorizer

簡介:A customizable extension for colorizing matching brackets

Code Runner

簡介:支持多種語言的代碼的立即執行。支持的語言: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 語法補全,語法高亮。

CMake Tools

簡介:CMake 工程構建工具。

這些插件的好處,自己打開網址查看。

7.2 調整VSCode設置

配置本機(Win10)用戶級setting.json:

VSCode > 文件 > 首選項 > 設置 > 用戶 > 點擊右上角一個圖標(見下圖) > 打開配置文件的json文件。

clip_image021

這里附上我的配置,個人認為還是比較優的,你也根據自己的需求調整:

{

"[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": "![${imageFileName}](${imageFilePath})",

"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

}

部分配置說明:

clip_image023

配置遠程服務器的setting.json:

clip_image025

這里附上我的配置,個人認為還是比較優的,你也根據自己的需求調整:

{

"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",

}

部分配置說明:

clip_image027

8. 題外話

環境搭建好之后,相關文件在遠程linux服務器的用戶根目錄下:

clip_image028

如果換了環境,又懶得重新安裝vscode server和插件,那么我們只需打包.vscode-server文件,然后拷貝到另一台服務器解壓到用戶根目錄下就OK了。

需要一個特別注意的問題:服務器的vscode server和本機的VSCode版本需要對應,若本機的VSCode更新了,那么你就連不上了,需要更新服務器的vscode server。

因為.vscode_server在根目錄下放着,所有可以一次配置一個組的人都用,但是要保證VSCode版本一樣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM