记录一下用VScode配置C++环境时遇到的坑。一开始是按照https://blog.csdn.net/bat67/article/details/76095813来配置的,结果后来出现退出代码为1的错误。后来是根据官方文档解决的。官方文档链接:https://code.visualstudio.com/docs/cpp/config-msvc
- 首先下载mingw-w64版本的,下载地址:https://sourceforge.net/projects/mingw-w64/
- 然后将bin所在文件夹加入到系统环境变量中。比如我的就是
- 接下来就是按照官方文档中的步骤来配置三个json文件。至于三个json文件是怎么生成的,我就不写了,可以参考上面那篇博客或者直接官方文档(* ̄︶ ̄)。
我的三个文件最后的代码分别是:
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "D:\\mingw-w64\\mingw32\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build hello world"//和tasks中label保持一致
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:\\mingw-w64\\mingw32\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
- 最后我的测试程序的输出结果:
这篇博客就是想记录一下这个坑,花了我一天时间。
后续敲代码的时候,应该是只要有.vscode这个文件夹,然后在哪个文件夹下面敲代码都可以。我新建了一个文件夹,又在下面新建了个子文件夹,结果cpp文件也可以编译。
调试程序: