目錄
一、下載VScode
1. 在官網直接下載安裝即可
2. 安裝中文插件
二、下載C語言編譯器MinGW-W64
三、配置編譯器環境變量
1. 解壓下載的壓縮包,復制該文件夾下bin目錄所在地址
2. 在電腦搜索環境變量并打開
3. 點擊環境變量→選擇系統變量里的Path→點擊編輯按鈕
4. 點擊新建
5. 將剛剛復制的環境變量粘貼進去
四. 測試環境變量配置是否成功
1. win+R輸入cmd進入控制臺頁面
2. 輸入以下代碼
3. 運行結果如下則配置成功
五、配置VScode
1. 安裝插件C/C++
2. 新建文件夾C作為C語言項目文件
2.1 新建.vscode文件包括下列四個文件
2.2 c_cpp_properties.json
2.3?launch.json
2.4?settings.json
2.5?tasks.json
3. 新建C++文件夾作為C++項目文件
3.1 新建.vscode文件包括下列四個文件
3.2 c_cpp_properties.json
3.3?launch.json
3.4?settings.json
3.5?tasks.json
六、重啟VScode
七、編寫C語言程序測試
1. 編寫hello.c文件
2. 右鍵選擇run code(需下載插件)
3. 成功運行代碼!
八、編寫C++程序測試
1. 編寫hello.cpp
2.? 右鍵選擇run code成功運行代碼
一、下載VScode
1. 在官網直接下載安裝即可
Visual Studio Code - Code Editing. Redefined
2. 安裝中文插件
二、下載C語言編譯器MinGW-W64
在我上傳的資源中可以免費下載!!!
三、配置編譯器環境變量
1. 解壓下載的壓縮包,復制該文件夾下bin目錄所在地址
2. 在電腦搜索環境變量并打開
3. 點擊環境變量→選擇系統變量里的Path→點擊編輯按鈕
4. 點擊新建
5. 將剛剛復制的環境變量粘貼進去
記得添加之后一路點擊確認!!!
四. 測試環境變量配置是否成功
1. win+R輸入cmd進入控制臺頁面
2. 輸入以下代碼
gcc --versiong++ --version
3. 運行結果如下則配置成功
五、配置VScode
1. 安裝插件C/C++
點擊左上角第五個圖標搜索下載
2. 新建文件夾C作為C語言項目文件
2.1 新建.vscode文件包括下列四個文件
2.2 c_cpp_properties.json
{"configurations": [{"name": "windows-gcc-x64","includePath": ["${workspaceFolder}/**"],"compilerPath": "D:/ItApp/mingw64/bin/gcc.exe", //替換自己電腦實際路徑"cStandard": "${default}","cppStandard": "${default}","intelliSenseMode": "windows-gcc-x64","compilerArgs": [""]}],"version": 4
}
2.3?launch.json
{"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"internalConsoleOptions": "neverOpen","MIMode": "gdb","miDebuggerPath": "gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": false}],"preLaunchTask": "Compile"},{"name": "C/C++: gcc.exe 生成和調試活動文件","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "D:\\ItApp\\mingw64\\bin\\gdb.exe", //替換自己電腦實際路徑"setupCommands": [{"description": "為 gdb 啟用整齊打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "將反匯編風格設置為 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}],"preLaunchTask": "C/C++: gcc.exe 生成活動文件"},{"name": "C/C++ Runner: Debug Session","type": "cppdbg","request": "launch","args": [],"stopAtEntry": false,"externalConsole": true,"cwd": "d:/VScode_WorkSpace/C", //替換自己電腦實際路徑"program": "d:/VScode_WorkSpace/C/build/Debug/outDebug", //替換自己電腦實際路徑"MIMode": "gdb","miDebuggerPath": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}
2.4?settings.json
{"C_Cpp_Runner.cCompilerPath": "gcc","C_Cpp_Runner.cppCompilerPath": "g++","C_Cpp_Runner.debuggerPath": "gdb","C_Cpp_Runner.cStandard": "","C_Cpp_Runner.cppStandard": "","C_Cpp_Runner.msvcBatchPath": "","C_Cpp_Runner.useMsvc": false,"C_Cpp_Runner.warnings": ["-Wall","-Wextra","-Wpedantic","-Wshadow","-Wformat=2","-Wcast-align","-Wconversion","-Wsign-conversion","-Wnull-dereference"],"C_Cpp_Runner.msvcWarnings": ["/W4","/permissive-","/w14242","/w14287","/w14296","/w14311","/w14826","/w44062","/w44242","/w14905","/w14906","/w14263","/w44265","/w14928"],"C_Cpp_Runner.enableWarnings": true,"C_Cpp_Runner.warningsAsError": false,"C_Cpp_Runner.compilerArgs": [],"C_Cpp_Runner.linkerArgs": [],"C_Cpp_Runner.includePaths": [],"C_Cpp_Runner.includeSearch": ["*","**/*"],"C_Cpp_Runner.excludeSearch": ["**/build","**/build/**","**/.*","**/.*/**","**/.vscode","**/.vscode/**"],"C_Cpp_Runner.useAddressSanitizer": false,"C_Cpp_Runner.useUndefinedSanitizer": false,"C_Cpp_Runner.useLeakSanitizer": false,"C_Cpp_Runner.showCompilationTime": false,"C_Cpp_Runner.useLinkTimeOptimization": false
}
2.5?tasks.json
{"tasks": [{"type": "cppbuild","label": "C/C++: gcc.exe 生成活動文件","command": "D:\\ItApp\\mingw64\\bin\\gcc.exe", //替換自己電腦實際路徑"args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "調試器生成的任務。"}],"version": "2.0.0"
}
3. 新建C++文件夾作為C++項目文件
3.1 新建.vscode文件包括下列四個文件
3.2 c_cpp_properties.json
{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"windowsSdkVersion": "10.0.17763.0","compilerPath": "D:\\ItApp\\mingw64\\bin\\g++.exe", /*修改成自己bin目錄下的g++.exe,這里的路徑和電腦里復制的文件目錄有一點不一樣,這里是兩個反斜杠\\*/"cStandard": "c11","cppStandard": "c++17","intelliSenseMode": "${default}"}],"version": 4
}
3.3?launch.json
{"version": "0.2.0","configurations": [{"name": "(gdb) Launch","preLaunchTask": "build","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb",// 這里修改為你電腦上安裝(解壓的文件 mingw64) 上的路徑, bin 文件名加上 gdb.exe"miDebuggerPath": "D:\\ItApp\\mingw64\\bin\\gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}
3.4?settings.json
{"files.defaultLanguage": "c", // ctrl+N新建文件后默認的語言"editor.formatOnType": true, // 輸入分號(C/C++的語句結束標識)后自動格式化當前這一行的代碼"editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳轉點,不用這個就必須手動觸發Intellisense了"editor.acceptSuggestionOnEnter": "off", // 我個人的習慣,按回車時一定是真正的換行,只有tab才會接受Intellisense// "editor.snippetSuggestions": "top", // (可選)snippets顯示在補全列表頂端,默認是inline"code-runner.runInTerminal": true, // 設置成false會在“輸出”中輸出,無法輸入"code-runner.executorMap": {"c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'","cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"// "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && $dir$fileNameWithoutExt",// "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc -std=c++17 -fexec-charset=GBK && $dir$fileNameWithoutExt"}, // 右鍵run code時運行的命令;未注釋的僅適用于PowerShell(Win10默認),文件名中有空格也可以編譯運行;注釋掉的適用于cmd(win7默認),PS和bash也能用,但文件名中有空格時無法運行"code-runner.saveFileBeforeRun": true, // run code前保存"code-runner.preserveFocus": true, // 若為false,run code后光標會聚焦到終端上。如果需要頻繁輸入數據可設為false"code-runner.clearPreviousOutput": false, // 每次run code前清空屬于code runner的終端消息,默認false"code-runner.ignoreSelection": true, // 默認為false,效果是鼠標選中一塊代碼后可以單獨執行,但C是編譯型語言,不適合這樣用"C_Cpp.clang_format_sortIncludes": true,"files.associations": {"array": "cpp","atomic": "cpp","*.tcc": "cpp","cctype": "cpp","clocale": "cpp","cmath": "cpp","cstdarg": "cpp","cstddef": "cpp","cstdint": "cpp","cstdio": "cpp","cstdlib": "cpp","cwchar": "cpp","cwctype": "cpp","deque": "cpp","unordered_map": "cpp","vector": "cpp","exception": "cpp","algorithm": "cpp","memory": "cpp","memory_resource": "cpp","optional": "cpp","string": "cpp","string_view": "cpp","system_error": "cpp","tuple": "cpp","type_traits": "cpp","utility": "cpp","fstream": "cpp","initializer_list": "cpp","iosfwd": "cpp","iostream": "cpp","istream": "cpp","limits": "cpp","new": "cpp","ostream": "cpp","sstream": "cpp","stdexcept": "cpp","streambuf": "cpp","typeinfo": "cpp"}, // 格式化時調整include的順序(按字母排序)
}
?
3.5?tasks.json
{"version": "2.0.0","tasks": [{"label": "build","type": "shell","group": "build","presentation": {"echo": true,"reveal": "always","focus": false,"panel": "shared"},"windows": {"command": "g++","args": ["-ggdb","${file}","--std=c++11","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"]}},{"type": "cppbuild","label": "C/C++: g++.exe 生成活動文件","command": "D:\\ItApp\\mingw64\\bin\\g++.exe", //替換自己電腦實際路徑"args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "調試器生成的任務。"}]
}
?
六、重啟VScode
配置完成之后一般重啟一下才會生效。
七、編寫C語言程序測試
1. 編寫hello.c文件
// hello.c#include <stdio.h>
int main()
{printf("hello world\n");return 0;
}
2. 右鍵選擇run code(需下載插件)
3. 成功運行代碼!
八、編寫C++程序測試
1. 編寫hello.cpp
#include <iostream>using namespace std;int main()
{cout << "hello world" << endl;return 0;
}