vscode是個不錯的編輯器,簡潔輕量,就是一開始的時候需要對環境進行配置,開個貼記錄一下怎么用MingGW64+clang來配置。
下載地址
- LLVM Download Page Pre-Built Binaries中的Clang for Window(64-bit),無需下載.sig文件
- MinGW-w64-for 32 and 64vbit Windows
安裝MinGW與Clang
Clang 下載完安裝,選擇Add LLVM to the system PATH for all users(一般重啟電腦,這一步才生效)。
MinGW安裝的時候Architecture選x86_64,裝好以后,復制到Clang的文件夾里,同時自己將MinGW的bin文件夾加到環境變量path中。
win+r,輸入cmd,打開輸入clang -v, 如果顯示版本號就安裝完成了。
下面是比較關鍵的四組代碼
記得把路徑根據自己的具體情況修改
launch.josn代碼:
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", // 配置名稱,將會在啟動配置的下拉菜單中顯示 "type": "cppdbg", // 配置類型,這里只能為cppdbg "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加) "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 將要進行調試的程序的路徑 "args": [], // 程序調試時傳遞給程序的命令行參數,一般設為空即可 "stopAtEntry": true, // 設為true時程序將暫停在程序入口處,我一般設置為true "cwd": "${workspaceFolder}", // 調試程序時的工作目錄 "environment": [], // (環境變量?) "externalConsole": true, // 調試時是否顯示控制台窗口,一般設置為true顯示控制台 "internalConsoleOptions": "neverOpen", // 如果不設為neverOpen,調試時會跳到“調試控制台”選項卡,你應該不需要對gdb手動輸命令吧? "MIMode": "gdb", // 指定連接的調試器,可以為gdb或lldb。但目前lldb在windows下沒有預編譯好的版本。 "miDebuggerPath": "gdb.exe", // 調試器路徑。 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "Compile" // 調試會話開始前執行的任務,一般為編譯程序。與tasks.json的label相對應 } ] }
tasks.josn代碼:
// https://code.visualstudio.com/docs/editor/tasks { "version": "2.0.0", "tasks": [ { "label": "Compile", // 任務名稱,與launch.json的preLaunchTask相對應 "command": "clang++", // 要使用的編譯器 "args": [ "${file}", "-o", // 指定輸出文件名,不加該參數則默認輸出a.exe "${fileDirname}/${fileBasenameNoExtension}.exe", "-g", // 生成和調試有關的信息 "-Wall", // 開啟額外警告 "-static-libgcc", // 靜態鏈接 "-fcolor-diagnostics", "--target=x86_64-w64-mingw", // 默認target為msvc,不加這一條就會找不到頭文件 "-std=c++17" // C語言最新標准為c11,或根據自己的需要進行修改 ], // 編譯命令參數 "type": "shell", "group": { "kind": "build", "isDefault": true // 設為false可做到一個tasks.json配置多個編譯指令,需要自己修改本文件,我這里不多提 }, "presentation": { "echo": true, "reveal": "always", // 在“終端”中顯示編譯信息的策略,可以為always,silent,never。具體參見VSC的文檔 "focus": false, // 設為true后可以使執行task時焦點聚集在終端,但對編譯c和c++來說,設為true沒有意義 "panel": "shared" // 不同的文件的編譯信息共享一個終端面板 } // "problemMatcher":"$gcc" // 如果你不使用clang,去掉前面的注釋符,並在上一條之后加個逗號。照着我的教程做的不需要改(也可以把這行刪去) } ] }
settings.json代碼:
{ "files.defaultLanguage": "cpp", // ctrl+N新建文件后默認的語言 "editor.formatOnType": true, // 輸入時就進行格式化,默認觸發字符較少,分號可以觸發 "editor.snippetSuggestions": "top", // snippets代碼優先顯示補全 "code-runner.runInTerminal": true, // 設置成false會在“輸出”中輸出,無法輸入 "code-runner.executorMap": { "c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt", "cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c++17 && $dir$fileNameWithoutExt" }, // 設置code runner的命令行 "code-runner.saveFileBeforeRun": true, // run code前保存 "code-runner.preserveFocus": true, // 若為false,run code后光標會聚焦到終端上。如果需要頻繁輸入數據可設為false "code-runner.clearPreviousOutput": false, // 每次run code前清空屬於code runner的終端消息 "C_Cpp.clang_format_sortIncludes": true, // 格式化時調整include的順序(按字母排序) "C_Cpp.intelliSenseEngine": "Default", // 可以為Default或Tag Parser,后者較老,功能較簡單。具體差別參考cpptools擴展文檔 "C_Cpp.errorSquiggles": "Disabled", // 因為有clang的lint,所以關掉 "C_Cpp.autocomplete": "Disabled", // 因為有clang的補全,所以關掉 "clang.cflags": [ // 控制c語言靜態檢測的參數 "--target=x86_64-w64-mingw", "-std=c11", "-Wall" ], "clang.cxxflags": [ // 控制c++靜態檢測時的參數 "--target=x86_64-w64-mingw", "-std=c++17", "-Wall" ], "clang.completion.enable": true, "files.associations": { "iostream": "cpp", "ostream": "cpp", "string_view": "cpp", "*.tcc": "cpp", "string": "cpp", "cmath": "cpp" } // 效果效果比cpptools要好 }
c_cpp_properties.json代碼:
{ "configurations": [ { "name": "MinGW", "intelliSenseMode": "clang-x64", "compilerPath": "C:/Program Files/LLVM/bin/gcc.exe", "includePath": [ "${workspaceFolder}" ], "defines": [], "browse": { "path": [ "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" }, "cStandard": "c11", "cppStandard": "c++17" } ], "version": 4 }