天天看點

VSCode連接配接遠端linux伺服器進行C++開發

1. 環境

· 1.52.1 (user setup)

· Win10

· 遠端linux伺服器

2. 安裝 VSCode
VSCode連接配接遠端linux伺服器進行C++開發

推薦下載下傳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檔案等待後續使用。

VSCode連接配接遠端linux伺服器進行C++開發

筆者這裡已經生成過id_rsa了,是以顯示的資訊不一樣。如果你以前生成過id_rsa了,那麼就不用再次生成了。這個id_rsa在本機VSCode連接配接遠端伺服器時認證使用。

4.2 配置VSCode和遠端伺服器的認證秘鑰

1、安裝完Remote - SSH後,VSCode左側欄會有「遠端資料總管」圖示,如果沒有就右單機打開

VSCode連接配接遠端linux伺服器進行C++開發

2、打開Remote -SSH配置檔案

VSCode連接配接遠端linux伺服器進行C++開發
VSCode連接配接遠端linux伺服器進行C++開發

配置完畢之後,「遠端資料總管」中會顯示你已經配置的伺服器:

VSCode連接配接遠端linux伺服器進行C++開發

3、打開遠端linux伺服器添加認證秘鑰

VSCode連接配接遠端linux伺服器進行C++開發

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

VSCode連接配接遠端linux伺服器進行C++開發
5. 在VSCode中打開遠端伺服器

在「遠端資料總管」選擇一個伺服器進行連接配接:

VSCode連接配接遠端linux伺服器進行C++開發
初次連接配接問題:

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

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

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

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

·

VSCode連接配接遠端linux伺服器進行C++開發

正确連接配接之後,VSCode左下角會顯示伺服器資訊:

VSCode連接配接遠端linux伺服器進行C++開發
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架構等資訊:

VSCode連接配接遠端linux伺服器進行C++開發

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

VSCode連接配接遠端linux伺服器進行C++開發
至此,我們的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檔案。

VSCode連接配接遠端linux伺服器進行C++開發

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

{

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

}

部配置設定置說明:

VSCode連接配接遠端linux伺服器進行C++開發
配置遠端伺服器的setting.json:
VSCode連接配接遠端linux伺服器進行C++開發

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

{

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

}

部配置設定置說明:

VSCode連接配接遠端linux伺服器進行C++開發
8. 題外話

環境搭建好之後,相關檔案在遠端linux伺服器的使用者根目錄下:

VSCode連接配接遠端linux伺服器進行C++開發
如果換了環境,又懶得重新安裝vscode server和插件,那麼我們隻需打包.vscode-server檔案,然後拷貝到另一台伺服器解壓到使用者根目錄下就OK了。 需要一個特别注意的問題:

伺服器的vscode server和本機的VSCode版本需要對應,若本機的VSCode更新了,那麼你就連不上了,需要更新伺服器的vscode server。

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