vscode C语言
下载编译器MinGW
- 链接,下载页面中选择 x86_64-win32-seh 下载
- 解压文件
- 在 系统变量 的 PATH 中添加 mingw/bin
- cmd输入 g++ --version 检查
安装插件
C/C++
代码提示
一般只需要安装C/C++即可,如果没有提示,那可能是以下原因
新建.vscode
.vscode文件夹存放配置,每次新工程都要复制进去
其中存放以下3个配置文件
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/App/UtilsTools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/App/UtilsTools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/App/UtilsTools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/App/UtilsTools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/App/UtilsTools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/App/UtilsTools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
], // 用于头文件,命令行 输入“g++ -v -E -x c++ -”查看
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64",
"compilerPath": "D:/App/UtilsTools/mingw64/bin/g++.exe"
}
],
"version": 4
}
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"preLaunchTask": "MY C/C++ Configurations", //调试前执行的任务,就是之前配置的tasks.json中的label字段
"type": "cppdbg", //配置类型,只能为cppdbg
"request": "launch", //请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", //调试程序的路径名称
"args": [], //调试传递参数
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, // false显示内置终端
"MIMode": "gdb",
"miDebuggerPath": "D:\\App\\UtilsTools\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "MY C/C++ Configurations",
"command": "D:/App/UtilsTools/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\App\\UtilsTools\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: D:/App/UtilsTools/mingw64/bin/g++.exe"
}
]
}
测试
// test.cpp
#include<iostream>
using namespace std;
int main() {
int a = 1, b = 2;
int tmp = a; // break
a = b; // break
b = tmp; // break
return 0;
}
可以在终端执行g++ .\test.cpp
和 .\test.exe
直接运行
也可以点击右上角的运行 或者 F5,然后选择调试配置 如下,preLauchTask名就是自己的配置名。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)