一、概述
一直想在Windows11系统下使用vscode运行C/C++程序,如果使用visual studio的话需要占用大量内存,且每次启动较慢。vscode则可以省去大多数烦恼。
二、具体配置过程
(一)参考博客
主要参考两篇博客,分别如下所示。第一篇博客我主要采用了其下载编译器的部分,第二篇博客我采用其vscode配置部分。(原因是因为根据第一篇博客,我没有根据他后面的配置思路配置成功)。
VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程_vscode 开发c++-CSDN博客
vscode配置C/C++环境(超详细保姆级教学)_vscode c++-CSDN博客
(二)vscode插件下载
我们需要准备一些vscode所必须的插件,如图所示。其中划红线的必须要安装上去。
(三)编译器下载
编译器下载可参考第一篇博客的下载指导。
VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程_vscode 开发c++-CSDN博客
这里,我不同于他的做法是将mingw64文件夹放到D盘下面了,如图所示,其余均相同。
(四)环境变量配置
打开设置,如图所示。
在下图左侧搜索环境变量,进入之后,点击右侧红圈。
保险起见,我们要将下面两个path均进行添加。
添加内容如图所示。为刚才下载编译器文件夹里面的bin文件夹的路径。
添加完成之后,需要特别注意的是,在关闭窗口时候,需要逐个点击确定,才可以完成保存,一定要点击确定才可以。
(五)vscode内部配置
1.概述
使用vscode编写编译C++文件,需要在里面添加如下的文件,如图所示。
需要建立.vscode文件夹,下面一共有四个文件,如下所示。可以使用vscode让软件帮忙生成,也可以直接手动建立。直接手动建立比较快速。这里我选择直接手动建立四个文件。
c_cpp_properties.json
launch.json
settings.json
tasks.json
下面将分别说明这四个文件里面的内容是什么。
2.c_cpp_properties.json
如下所示。
{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"compilerPath": "D:/mingw64/bin/g++.exe","cStandard": "c17","cppStandard": "gnu++14","intelliSenseMode": "gcc-x64"}],"version": 4
}
图中红色画出来的地方需要替换为自己的g++编译器的路径,才可以正常运行。
3. launch.json
如下所示。需要注意这里gdb.exe文件的路径同样也需要换成自己的路径。
{"version": "0.2.0","configurations": [{"name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false,"cwd": "${workspaceRoot}","environment": [],"externalConsole": true, "MIMode": "gdb","miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe","preLaunchTask": "g++","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]}
4.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}
5.tasks.json
如下所示。需要注意将路径名称更改为自己的路径名称
{"version": "2.0.0","tasks": [{"label": "g++","command": "g++","args": ["-g","${file}","-o","${fileDirname}/${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++: g++.exe 生成活动文件","command": "D:/mingw64/bin/g++.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "D:/mingw64/bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "调试器生成的任务。"}]
}
(六)运行演示
1.建立cpp文件
#include<stdio.h>int main()
{printf("hello world!\n");return 0;
}
2.运行文件
如图所示,点击运行后即可运行,下面是截图
3.调试文件debug
如图所示,在运行时候会有调试选项,选择调试C/C++文件,即可进行调试。