
cpp在vsCode中的编译运行与调试
cpp在vsCode中的编译运行与调试
cpp在vsCode中的编译运行与调试
1、MinGW-w64下载
- 进入MinGW-w64下载地址:https://www.mingw-w64.org/downloads/。往下划找到下载入口,进入
- 在https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/,往下划找到
我是64位的Windwos系统,所以我选择x86_64-win32-seh下载。 - 倒计时完毕,开始下载
- 把MinGW压缩包解压到你自己喜欢的目录里边就好。然后配置环境变量即可:
1、打开MinGW中的bin文件夹,复制路径。我的bin路径为:C:\Program Files\mingw64\bin
2、把这个路径加到环境变量的path中即可。
3、随后在win + r,输入cmd。打开命令行窗口
4、输入where gcc。出现C:\Program Files\mingw64\bin\gcc.exe。即MinGW配置成功!
2、打开vsCode安装C++插件
3、配置编译器
在一个你喜欢的位置新建一个存放cpp项目的文件夹,并用vsCode打开该文件夹,创建一个与文件夹名称相同的cpp文件。
设置编译选项
鼠标停留在当前的cpp文件中,按Ctrl+Shift+p快捷键,输入c/c++
4、编译配置(配置tasks.json)
回到刚刚创建的cpp文件中,在导航栏点击terminal找到Configure Default Build Task…
在出现的弹窗中,选择C/C++:g++.exe build active file
此时在项目文件夹中出现.vscode文件夹。生成的cpp编译的配置文件tasks.json就在其中。它相当于编译指南,在该项目编译程序之前会先看一下tasks.json中的编译设置。
//以下是我生成的tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw64\\bin\\g++.exe\""
}
]
}
编译单个.cpp文件
来到要进行编译的mytest.cpp文件,在导航栏中点击Terminal,找到Run Build Task。就会根据我们的tasks.json中的编译设置,对cpp文件进行编译,生成mytest.exe程序。默认生成的tasks.json只能编译一个cpp文件
运行编译后的mytest.exe程序。Ctrl + `,打开终端。输入./可执行文件名.exe
。或 直接输入可执行文件名.exe
编译多个cpp文件
如果说,我们的mytest项目文件夹下有多个cpp文件,那么需要修改tasks.json中的参数:
默认的tasks.json文件
修改:1、可以编译项目文件夹下多cpp文件;2、输出的可执行文件名称为项目文件夹名称。
将 ${file}改为${workspaceFolder}\\.*cpp
将 ${fileDirname}\\${fileBasenameNoExtension}.exe改为${workspaceFolder}\\${workspaceRootFolderName}.exe
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw64\\bin\\g++.exe",//编译程序
"args": [//程序参数
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}\\.*cpp",//需要编译的文件名称
"-o",
"${workspaceFolder}\\${workspaceRootFolderName}.exe"//编译后的可执行文件名称
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw64\\bin\\g++.exe\""
}
]
}
调试配置(配置 launch.json)
1、在侧边栏找到debug,创建launch.json文件
2、然后选择C++(GDB/LLDB)
在.vscode文件夹下出现launch.josn文件
添加launch.json文件的配置,在导航栏找到Run,点击Add Configuration
选择C/C++:(gdb) Launch
默认的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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
//"program": "需要调试的可执行文件名称",这里与我们上边设置tasks.json文件中编译后的可执行文件名称相同
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
//"miDebuggerPath": "Mingw64的bin目录下gdb.exe路径",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
修改:
1、 “program”:“调试的可执行文件的名称”,这里与我们上边设置tasks.json文件中编译后的可执行文件名称相同
2、“miDebuggerPath”: “Mingw64的bin目录下gdb.exe路径”,
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
//这里与我们上边设置tasks.json文件中编译后的可执行文件名称相同
"program": "${workspaceFolder}/${workspaceRootFolderName}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
//Mingw64的bin目录下gdb.exe路径
"miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
开始调试
配置完Launch.json文件后,我们可以打断点开始调试。
F5:启动调试
F9:打断点/取消断点
F11:逐语句调试
F10:逐过程调试
更多推荐
所有评论(0)