VS Code配置C/C++环境
前言
vscode是一个轻量级比较好用的一款编辑器,界面清洁简单,但需要进行很多环境配置,因此再次记录vscode配置C/C++的过程。
安装vscode
直接在官网进行下载,下载完成后进行安装,傻瓜式安装即可。ps:建议放到除C盘外(当然内存够当我没说)。
安装MinGW-W64
MinGW-W64 可以去MinGW-w64的官网下载,也就可以直接去 MinGW-W64 的 GitHub 上下载。由于在官网下载容易下错,所以我这里给一个 GitHub 的链接,也是在 VS Code 上提供的链接。
MinGW-W64下载链接:Releases · msys2/msys2-installer (github.com)
进入链接后,可以看到历史版本的更替,截至本教程编写日期,最新版本为 2025-02-21 的版本,单击日期跳转至下载窗口。
配置环境
找到刚刚安装MinGW-W64的路径,添加到环境变量中,先找到path:
然后点击编辑,然后新建,添加路径:
接着点三个确定,结束。
验证:打开cmd,在里面输入:g++ --version ,出现版本号即为成功!
安装vs code插件
在扩展商店里面搜索:C/C++,而C/C++ Extension Pack是 C/C++ 的扩展功能包,里面包含了一些项目管理和代码构建的工具,不是必要的扩展包,可以选择性安装。
然后先建立一个项目,新建文件test.c,写一个c程序,进行运行,选择C/C++(GDB/LLDB),就会新生成一个.vscode文件夹,里面会出现json文件,默认会有一个launch.json,其余自己配置:
配置相关文件
1.c_cpp_properties.json
{
"configurations": [
{
"name": "Win64",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "D:/develop/MinGW/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
2.launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// "program": "${fileDirname}\\coin\\${fileBasenameNoExtension}.exe",
// "args": [],
"program":"C:\\Windows\\System32\\cmd.exe",
"args": ["/c","${fileDirname}/coin/${fileBasenameNoExtension}.exe","&","pause"],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\develop\\MinGW\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
3.settings.json
{
"files.associations": {
"*.py": "python",
"iostream": "cpp",
"*.tcc": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"ostream": "cpp",
"new": "cpp",
"typeinfo": "cpp",
"deque": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"sstream": "cpp",
"map": "c",
"stdio.h": "c",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"ios": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"stdlib.h": "c",
"string.h": "c"
},
"editor.suggest.snippetsPreventQuickSuggestions": false,
"aiXcoder.showTrayIcon": true
}
4.tsaks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "g++",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/coin/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:\\develop\\MinGW\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\coin\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
其中代码中的:D:\\develop\\MinGW\\mingw64\\bin,都要改成自己的路径,现在就可以运行了,结束!
本页面最后更新于:2025-03-14 16:15:18,距今已 44 天,如有链接失效,您可在下方评论区留言!
文章标题:VS Code配置C/C++环境
原文地址:https://www.031803.xyz/archives/152.html
版权声明:文章版权归作者所有,未经允许禁止转载