VsCode的配置难点就在于配置文件,网上很多教程给出的配置文件需要读者根据自己的安装路径修改,这对于新手来说极为不友好,且新手往往不明白配置文件中代码的含义,导致很多新手的配置文件出错,始终无法调试成功第一个程序。
大多数时候,配置文件可以在程序调试通过后慢慢熟悉修改,因此本文给出了一种配置文件,无需任何修改!直接复制粘贴,你的程序就能运行。
一、安装VsCode
VsCode下载地址:Visual Studio Code - Code Editing. Redefined 下载后正常安装即可
二、安装配置mingw64
mingw64下载地址:MinGW-w64
下载第一个即可,解压到你想放的路径,然后添加到环境变量中
打开cmd,输入gcc -v,如果出现如下图所示的一大篇,说明配置成功!
三、安装C/C++拓展
如下图所示,打开VsCode,在左侧点击拓展——>输入C搜索——>选择图中那一个——>在右侧单机Install进行安装
四、添加配置文件
打开VsCode,.vscode文件夹和其中的launch.json和tasks.json你可以手动创建。
直接复制下面的配置文件内容到launch.json和tasks.json中,无需修改。
launch.json配置如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) 启动",
"type": "cppvsdbg",
"request": "launch",
"program": "C:\\Windows\\System32\\cmd.exe",//"program":"${fileDirname}\\z.exe",
"args": ["/C","${fileDirname}/z.exe","&","pause"],//${fileDirname}为生成的exe文件路径
//"stopAtEntry": false,
"cwd": "${fileDirname}",
//"miDebuggerPath": "D:\\Program\\mingw64\\bin\\gdb.exe",
//"environment": [],
"console": "externalTerminal",
// "MIMode": "gdb",
// "setupCommands": [
// {
// "description": "为 gdb 启用整齐打印",
// "text": "-enable-pretty-printing",
// "ignoreFailures": true,
// }
// ],
"preLaunchTask": "complie"
}
]
}
tasks.json配置如下:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "complie",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",//源文件名称
"-o",
"${fileDirname}\\z.exe"//生成的文件名称
],
// "presentation": {
// "reveal": "silent"
// },
//"problemMatcher": "$msCompile"
}
]
}