前言
在配置vscode环境的过程中,博主遇到了很多困难,并且在查阅后得不到解决(想重装系统了就是说),最后的最后终于成功运行了,在此做一点总结,希望分享给需要的小伙伴。
这个方法并不能运行包含有.h文件的代码,因为博主只是想用vscode写写算法题或者看看代码,而项目则用visual studio,因此已经足够了。
操作步骤
1.去官网根据电脑相关系统类型下载mingw。
右键此电脑 --> 在属性中点击高级系统设置 --> 点击环境变量
--> 新建系统变量 --> 变量名为Path,变量值设置为之前下载好mingw中bin的目录地址
2.下载C/C++插件。
点击左边工具栏中扩展 --> 找到插件并下载
3.配置文件 (最关键一步)
随意新建一个文件夹xxx --> 再在文件夹里面新建一个 .vscode --> 用vscode打开xxx文件夹
-->手动添加改名如图三个文件 -->
1)c_cpp_properties.json
注意:在“comilerPath” 后面添加你所下载的g++.exe的地址
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
//此处是编译器路径,以后可直接在此修改
"compilerPath": "C:/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
2)launch.json
同样在“miDebuggerPath” 后面修改相应个gdc.exe地址
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
"type": "cppdbg",//配置类型,只能为cppdbg
"request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
"args": [],//调试传递参数
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,//true显示外置的控制台窗口,false显示内置终端
"MIMode": "gdb",
"miDebuggerPath": "C:/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
3)tasks.json
"command"和"cwd"中修改地址
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file", //这里注意一下,见下文
"command": "C:/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
4.然后就试着打印出"hello world"啦
总结
可以将你配置的xxx文件压缩,之后需要在vscode上打代码时,只需要将压缩包中的xxx拖拽到桌面并改名(自己命名),拖到vscode就可以啦。