1.下載llvm和mingw64
將 mingw64 壓縮包內文件全部解壓到 llvm 的安裝文件夾,默認是 C:\Program Files\LLVM
2.vscode下載c/c++插件
3.vscode配置
新建項目文件夾,新建.vscode文件夾,添加以下json文件
- c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW",
"intelliSenseMode": "gcc-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
}
- launch.json
// 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": 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": [ // 用處未知,模板如此
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile" // 調試會話開始前執行的任務,一般爲編譯程序。與tasks.json的label相對應
}
]
}
- settings.json
{
"files.defaultLanguage": "c", // 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 --target=x86_64-w64-mingw -std=c11 && &'$dir$fileNameWithoutExt'",
"cpp": "cd $dir && clang++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -Og -static-libgcc --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": {
"cfloat": "cpp",
"cstdbool": "cpp"
} // 效果比cpptools要好
}
- tasks.json
// https://code.visualstudio.com/docs/editor/tasks
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile", // 任務名稱,與launch.json的preLaunchTask相對應
"command": "clang++", // 要使用的編譯器,C++用clang++;如果編譯失敗,改成gcc或g++試試,還有問題那就是你自己的代碼有錯誤
"args": [
"${file}",
"-o", // 指定輸出文件名,不加該參數則默認輸出a.exe,Linux下默認a.out
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g", // 生成和調試有關的信息
"-Wall", // 開啓額外警告
"-static-libgcc", // 靜態鏈接libgcc
"--target=x86_64-w64-mingw", // clang的默認target爲msvc,不加這一條就會找不到頭文件;Linux下去掉這一條
"-std=c++17" // C++最新標準爲c++17,或根據自己的需要進行修改
], // 編譯命令參數
"type": "process", // process是vsc把預定義變量和轉義解析後直接全部傳給command;shell相當於先打開shell再輸入命令,所以args還會經過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,去掉前面的註釋符,並在上一條之後加個逗號。照着我的教程做的不需要改(也可以把這行刪去)
}
]
}
4.測試
添加 test.c 或 test.cpp 文件
#include <iostream>
using namespace std;
int main()
{
cout << "Test End" << endl;
getchar();
return 0;
}
F5 調試