文章目錄
很多人都把 vs code(vsc) 作爲一個好康的編輯器來用(yysy確實比vim、emacs好看多了)不過一個 開源的、跨平臺的 的vs code,更應該發揮出“不是IDE優於IDE”的力量。
下面以 cpp 開發環境爲例,介紹一下 vsc 的配置過程。
打開 vsc 之前
首先是環境變量,win 下是用 MinGW-w64 - for 32 and 64 bit Windows
,選新版本的 x86_64-posix-seh
。
macOS 相對簡單,Xcode 大法完事···
選擇插件
vsc 的 extension 商城琳琅滿目的插件,不過其實就兩個足矣。
有了這兩個插件,就可以準備編譯運行了!
json 文件
有 3 個 json
文件是與編譯運行相關的,(基本上覆制粘貼就行)分別是:
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
], // 這個路徑改成自己的(但好像不是很重要?我現在沒有Xcode了但是還能正常編譯
"compilerPath": "/usr/bin/clang", // 這個很重要,替換成自己的
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
如果你不知道你的 clang
在哪裏,可以
$ whereis clang
win 是你自己下載的 mingw 的路徑。。。
launch.json
win 版
{
"version": "0.2.0",
"configurations": [{
"name": "(gdb) Launch", // 配置名稱,將會在啓動配置的下拉菜單中顯示
"type": "cppdbg", // 配置類型,cppdbg對應cpptools提供的調試功能;可以認爲此處只能是cppdbg
"request": "launch", // 請求配置類型,可以爲launch(啓動)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 將要進行調試的程序的路徑
"args": [], // 程序調試時傳遞給程序的命令行參數,一般設爲空即可
"stopAtEntry": false, // 設爲true時程序將暫停在程序入口處,相當於在main上打斷點
"cwd": "${workspaceFolder}", // 調試程序時的工作目錄,此爲工作區文件夾;改成${fileDirname}可變爲文件所在目錄
"environment": [], // 環境變量
"externalConsole": true, // 爲true時使用單獨的cmd窗口,與其它IDE一致;18年10月後設爲false可調用VSC內置終端
"internalConsoleOptions": "neverOpen", // 如果不設爲neverOpen,調試時會跳到“調試控制檯”選項卡,你應該不需要對gdb手動輸命令吧?
"MIMode": "gdb", // 指定連接的調試器,可以爲gdb或lldb。但我沒試過lldb
"miDebuggerPath": "gdb.exe", // 調試器路徑,Windows下後綴不能省略,Linux下則不要
"setupCommands": [
{ // 模板自帶,好像可以更好地顯示STL容器的內容,具體作用自行Google
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile" // 調試會話開始前執行的任務,一般爲編譯程序。與tasks.json的label相對應
}]
}
Mac 版
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) 啓動",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false, // 設爲true時程序將暫停在程序入口處,相當於在main上打斷點
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"miDebuggerPath": "/usr/bin/clang",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
// "preLaunchTask": "Compile with g++",
}
]
}
tasks.json
win
{
"version": "2.0.0",
"tasks": [{
"label": "Compile", // 任務名稱,與launch.json的preLaunchTask相對應
"command": "gcc", // 要使用的編譯器,C++用g++
"args": [
"${file}",
"-o", // 指定輸出文件名,不加該參數則默認輸出a.exe,Linux下默認a.out
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g", // 生成和調試有關的信息
"-Wall", // 開啓額外警告
"-static-libgcc", // 靜態鏈接libgcc,一般都會加上
"-fexec-charset=GBK", // 生成的程序使用GBK編碼,不加這一條會導致Win下輸出中文亂碼
// "-std=c11", // C++最新標準爲c++17,或根據自己的需要進行修改
], // 編譯的命令,其實相當於VSC幫你在終端中輸了這些東西
"type": "process", // process是vsc把預定義變量和轉義解析後直接全部傳給command;shell相當於先打開shell再輸入命令,所以args還會經過shell再解析一遍
"group": {
"kind": "build",
"isDefault": true // 不爲true時ctrl shift B就要手動選擇了
},
"presentation": {
"echo": true,
"reveal": "always", // 執行任務時是否跳轉到終端面板,可以爲always,silent,never。具體參見VSC的文檔
"focus": false, // 設爲true後可以使執行task時焦點聚集在終端,但對編譯C/C++來說,設爲true沒有意義
"panel": "shared" // 不同的文件的編譯信息共享一個終端面板
},
// "problemMatcher":"$gcc" // 此選項可以捕捉編譯時終端裏的報錯信息;但因爲有Lint,再開這個可能有雙重報錯
}]
}
Mac
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary",
// "${file}",
// "-g", // 生成和調試有關的信息
// "-Wall", // 開啓額外警告
"-o", // 指定輸出文件名,不加該參數則默認輸出a.exe,Linux下默認a.out
"${fileDirname}/${fileBasenameNoExtension}.out",
"-static-libgcc" // 靜態鏈接libgcc,一般都會加上
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
一些輔助性的設置
好康的擴展
這個會讓你有明亮的括號
這個是一個比較省眼睛的主題
還有就是:
settings.json
- 在左下角找設置
- 右上角打開 json(就是三角形右邊那個按鈕)
- 然後選擇你需要的部分輸入進去
{
"files.defaultLanguage": "cpp", // ctrl+N新建文件後默認的語言
"files.trimTrailingWhitespace": true, // 保存時,刪除每一行末尾的空格
"files.insertFinalNewline": true,// 保存後文件最末尾加一整行空行,Linux下的習慣
"C_Cpp.updateChannel": "Insiders",
"http.proxySupport": "off",
"vsicons.dontShowNewVersionMessage": true,
"window.zoomLevel": -1,
"workbench.iconTheme": "vscode-icons",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"workbench.colorTheme": "One Dark Pro",
// editor
"editor.fontSize": 21,
"editor.largeFileOptimizations": false,
"editor.dragAndDrop": true,
"editor.formatOnType": true, // 輸入時就進行格式化,默認觸發字符較少,分號可以觸發
"editor.snippetSuggestions": "top", // snippets代碼優先顯示補全
"editor.suggestSelection": "first",
"editor.insertSpaces": false,
"editor.cursorStyle": "line-thin",
"editor.formatOnPaste": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.fontLigatures": true, // 連體字,效果不太好形容,見 https://typeof.net/Iosevka 最後一部分
"editor.cursorSmoothCaretAnimation": true, // 移動光標時變得平滑
"editor.smoothScrolling": true, // 滾動平滑,不過效果很微弱
// "editor.fontFamily": "Menlo, Monaco, 'Courier New', monospace",
"editor.fontFamily": "Consolas, 'Courier New', monospace",
"editor.fontWeight": "normal",
// code-runner
"code-runner.runInTerminal": true, // 設置成false會在“輸出”中輸出,無法輸入
"code-runner.preserveFocus": true, // 若爲false,run code後光標會聚焦到終端上。如果需要頻繁輸入數據可設爲false
"code-runner.clearPreviousOutput": false,
"terminal.integrated.cursorBlinking": true,
"terminal.integrated.cursorStyle": "line",
"terminal.integrated.fontSize": 20,
// "code-runner.executorMap": {
// // "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.out' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
// // "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.out' -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
// "c": "cd $dir; clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11; ./$fileNameWithoutExt",
// "cpp": "cd $dir; clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c++17; ./$fileNameWithoutExt"
// }, // 設置code runner的命令行
// python
"python.pythonPath": "/usr/local/bin/python3",
"diffEditor.ignoreTrimWhitespace": true,
"debug.allowBreakpointsEverywhere": true,
// latex
"latex-workshop.latex.recipes": [
{
"name": "xelatex",
"tools": [
"xelatex"
]
},
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex*2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
}
],
"latex-workshop.latex.tools": [
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
},
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],
"latex-workshop.view.pdf.viewer": "tab",
"latex-workshop.latex.clean.fileTypes": [
"*.aux",
"*.bbl",
"*.blg",
"*.idx",
"*.ind",
"*.lof",
"*.lot",
"*.out",
"*.toc",
"*.acn",
"*.acr",
"*.alg",
"*.glg",
"*.glo",
"*.gls",
"*.ist",
"*.fls",
"*.log",
"*.fdb_latexmk"
],
}
好了到此結束,享受你的 visual studio code 吧!
哦對了,如果想更深瞭解,可以參考這條