随手笔记前言
vscode在windows系统上进行C/C++环境配置
步骤如下
第一步 下载安装VSCode
这应该是最简单的一步,相信大家自己就可以完成。如果在vscode官网感觉下载特别慢的话,可以去试一下腾讯软件中心,我都是在这个网页上下载的。下载好之后根据提示安装就可以了。
第二步 下载安装g++
MinGW官网:该网站下载MinGW软件包
下载完成后进行安装,自己选择安装路径,安装的路径需要记住,马上就要用到
注意路径中不能出现中文!注意路径中不能出现中文!注意路径中不能出现中文!
之后我们进入设置,搜索“环境变量”,设置环境变量
然后我们再来看一看刚刚的操作有没有成功,按Win+R,输入cmd,在控制台中输入g++ --version
出现这样的界面就算成功了
第三步 安装VSCode插件
现在我们打开我们刚刚安装好的VSCode,点击这个按钮
搜索C/C++ 安装第一个插件(我之前已经配置过了所以已经安装好了)
再搜索安装Code Runner
如果需要中文版的同学可以搜索安装中文插件 (个人不推荐,新手如果感觉入门困难可以安装)
还有很多好用的插件,比如自动补齐,括号换颜色等,大家有需要可以自己搜一搜。
第四步 配置调试功能
这一步非常重要,请谨慎认真处理。
首先大家在一个你希望的位置建一个文件夹,随意起名就可以(注意不可以用中文!),以后的C/C++代码文件都要放在这个文件夹里才可以正常调试。
这里我就建在桌面上了。
然后进入VSCode,点击Open Folder或者点击左上角File -> Open Folder,然后打开刚刚建的文件夹,选择信任父级文件夹
点击这个图标新建一个文件夹,命名为.vscode(注意必须是这个名字!)
创建完成后再点击这个图标新建四个文件
四个文件分别是:
1. c_cpp_properties.json
2. launch.json
3. settings.json
4. tasks.json
接下来复制粘贴这四个文件的内容
首先是c_cpp_properties.json
{"configurations": [{"name": "Win64","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG", "UNICODE", "_UNICODE"],"windowsSdkVersion": "10.0.18362.0","compilerPath": "C:/MySoftware/MinGW/MinGW/bin/g++.exe","cStandard": "c17","cppStandard": "c++17","intelliSenseMode": "gcc-x64"}],"version": 4
}
注意compilerPath这一项要把路径改成刚才g++的安装路径:找到刚刚的安装文件夹->C:/MySoftware/MinGW/MinGW/bin/g++.exe ,然后复制或者手动把g++.exe的路径敲上去,格式要跟上面代码段一样
然后是launch.json
{"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": "C:\\MySoftware\\MinGW\\MinGW\\bin\\gdb.exe","preLaunchTask": "g++","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]}
注意miDebuggerPath这一项也要把路径C:\MySoftware\MinGW\MinGW\bin\gdb.exe ,然后复制或者手动把gdb.exe的路径敲上去,格式要跟上面代码段一样
接下来是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
}
最后是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": {"kind": "build","isDefault": true}}]
}
保存这四个文件就配置完成了!
再次强调:以后的C/C++代码文件必须放在这个Code文件夹里,或者说有.vscode文件夹的文件夹里,如果调试放在其他位置的代码文件会报错!
可以像我这样在Code文件中建多个文件夹分类存放代码。
如果上述流程你都完成了,那么现在你已经可以新建一个.c或者.cpp文件写代码测试一下你刚刚配置好的VSCode啦!(注意文件名也不能用中文!)
如果调试时显示“找不到g++”
如果不存在中文的话,右键点击VSCode的图标,选择“属性”
然后选择“兼容性”,勾选“以管理员身份运行此程序” ,然后依次点击“应用”,‘确定’即可(部分电脑需要选择这个选项)
运行和调试的时候,是要点右侧三个点的