vscode用MinGW和Clang配置C++環境

 

 vscode是個不錯的編輯器,簡潔輕量,就是一開始的時候須要對環境進行配置,開個貼記錄一下怎麼用MingGW64+clang來配置。html

下載地址ios

 

安裝MinGW與Clangc++

Clang 下載完安裝,選擇Add LLVM to the system PATH for all users(通常重啓電腦,這一步才生效)。git

MinGW安裝的時候Architecture選x86_64,裝好之後,複製到Clang的文件夾裏,同時本身將MinGW的bin文件夾加到環境變量path中。github

 

win+r,輸入cmd,打開輸入clang -v, 若是顯示版本號就安裝完成了。shell

 

 

下面是比較關鍵的四組代碼json

 

 

記得把路徑根據本身的具體狀況修改windows

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代碼:ui

// 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 }
相關文章
相關標籤/搜索