- gcc: C語(yǔ)言編譯器
- g++: C++編譯器
- gdb: 執(zhí)行器
下載MinGW64
網(wǎng)盤分享地址如下 離線下載包冤寿,
鏈接:https://pan.baidu.com/s/1hJR8BPHSdgd8vjDfu0A6jw 密碼:1wz9
- 下載完之后解壓
- 添加路徑到環(huán)境變量,用戶變量下的path金矛,如:D:\Program Files\mingw64\bin
安裝vscode
- 官網(wǎng)下載然后安裝
- 安裝拓展:打開(kāi)extensions,搜索c++,安裝然后reload
新建或打開(kāi)c++文件
- 新建文件夾,新建hello.cpp文件
- 編寫一個(gè)簡(jiǎn)單的文件
//c++
#include <iostream>
using namespace std;
int main(){
cout<<"..agacb....hellhghoworldghg.."<<endl;
//system("pause");
return 0;
}
//c
#include <stdio.h>
int main() {
int i = 0;
printf("Hello World");
}
生成launch.json文件
- 瀏覽到調(diào)試的窗口强饮,去添加配置。選擇C++(GDB/LIDB)为黎,生成launch.json文件邮丰。
- 將launch.json文件內(nèi)容更改如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 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": false,//true的話會(huì)彈出命令行
"MIMode": "gdb",
"miDebuggerPath": "D:/Program Files/mingw64/bin/gdb.exe",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
生成tasks.json文件
這個(gè) task.json 的作用就是, 在通過(guò) launch,json 里的配置運(yùn)行之前, 先根據(jù)我們自定義的命令去運(yùn)行一個(gè)任務(wù)
在這里通常這個(gè)任務(wù)是編譯, 也就是把我們的 .cpp 變成 .exe, 這樣 launch.json 才可以用 .exe 去執(zhí)行
- 打開(kāi)命令面板(Ctrl + Shift + P)。
- 選擇Tasks:Configure Tasks ...命令
- 單擊從模板創(chuàng)建tasks.json文件
- 您將看到任務(wù)運(yùn)行模板列表,選擇Others
- 將文件內(nèi)容修改如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe"http://沒(méi)有后綴
],
}
]
}
- 此操作跟在命令行輸入-g hello.cpp -o hello.exe的作用一樣
- Task 完成了之后, 就開(kāi)始 launch.json 的運(yùn)行了
- 會(huì)根據(jù) launch.json 里的 miDebuggerPath 配置的 編譯器路徑去運(yùn)行上剛生成的 exe 文件
配置c_cpp_properties.json文件
- 應(yīng)該是配置 cpp 文件編譯時(shí)的全局配置吧, 也好像是提供智能感知的配置
- 通過(guò)快捷方式Ctrl+Shift+P運(yùn)行C/CPP: Edit configuration ...命令添加缺少的信息并生成c_cpp_properties.json文件铭乾。
- 配置如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/Program Files/mingw64/bin/gcc.exe",
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
- "compilerPath": "D:/Program Files/mingw64/bin/gcc.exe",添加這一條剪廉,設(shè)置gcc.exe的路徑(根據(jù)自己實(shí)際gcc.exe路徑添加)。
- 直觀的效果就是引用頭文件出現(xiàn)的綠色波浪線沒(méi)了炕檩。
F5成功編譯
若是上述操作失敗可以重新新建另一個(gè)文件夾來(lái)嘗試或者刪掉.vscode文件夾再來(lái)過(guò)斗蒋。