獲取C/C++擴展
- 打開vscode
- ctrl+shift+x打開商店
- 搜索C/C++
- 安裝、重啟vscode
安裝GCC
- 下載MinGW
- 打開安裝程序,安裝到D盤(需要創建MinGW文件夾),安裝完成之后生成MinGW安裝管理器
- 管理器自動打開(如果沒有請手動),點擊
All Packages
,選中gcc.bin,g++.bin,gdb.bin,點擊Installation
,選擇Apply Changes
,點擊Apply
提交進行安裝
- win+R,輸入control進入控制面板,依次點擊
系統與安裝
->系統
->高級系統設置
->環境變量
,找到Path
->編輯
->新建
,將安裝好的MinGW的bin文件夾路徑粘貼進去。
配置智能提示
- 編寫一個測試文件demo.c,點擊引入的標准庫下的“提示燈”,生成c_cpp_properties.json文件
- 編輯c_cpp_properties.json
c_cpp_properties.json修改內容如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "8.1",
"compilerPath": "D:\\MinGW\\bin\\gcc.exe",// 自己電腦上的gcc路徑
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
開啟調試功能
- F5或Ctrl+F5啟用調試,選擇C++(GDB/LLDB)生成launch.json文件,修改如下:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 被調試程序
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",// 自己電腦的gdb
"preLaunchTask": "echo",// 在調試前需要執行的任務名稱
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
- Ctrl+Shift+P輸入Tasks:Configure Task配置任務,選擇使用模塊創建task.json文件,選擇Others模板,生成task.json文件,修改如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",// 任務名稱
"type": "shell",
"command": "gcc",
"args": [
"-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"// 生成可調試的執行文件
]
}
]
}
如果出現錯誤:進程終止,退出代碼:1,可能是gcc還沒被編輯器加載(測試方法:打開控制台,輸入gcc,看提示信息),重啟編輯器就可以。