参考帖子:
帖子1
帖子2
前言
本人是个vscode小白害怕配环境,但无奈不用调试找bug太麻烦了。于是就在csdn搜集资料。帖子还是很多的,但复制粘贴到自己的电脑上总是会觉得,自己的vscode怎么跟别人的不一样/(ㄒoㄒ)/~~
折腾了一下午,总算搞定了。幸亏成功了,不然我的心态可能就爆炸了。。。
步骤
1、创建并编辑aunch.json
打开左侧的“调试”,点击新建launch.json文件夹。
或者点击上方任务栏的 运行 —>新建配置文件,则在资源管理器中建立了一个.vscode文件夹。
将里面替换成以下代码
{
// 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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", //程序文件路径
"args": [], //程序运行需传入的参数
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true, //运行时是否显示控制台窗口
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
2、创建tasks.json
在上一步的基础上,将窗口切换至test.cpp,并按F5,得到下图1
点击配置任务后,得到下图2
点击使用模板创建tasks.json文件,得到图三,并选择Others
则创建了tasks.json,将里面的代码替换为
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file", /* 与launch.json文件里的preLaunchTask的内容保持一致 */
"command": "/usr/bin/g++",
"args": [
"-std=c++11",
"-g",
"${file}", /* 编译单个文件 */
//"${fileDirname}/*.cpp", /* 编译多个文件 */
"-o",
"${fileDirname}/${fileBasenameNoExtension}", /* 输出文件路径 */
/* 项目所需的头文件路径 */
"-I","${workspaceFolder}/",
"-I","/usr/local/include/",
"-I","/usr/local/include/opencv4/",
"-I","/usr/local/include/opencv4/opencv2",
/* 项目所需的库文件路径 */
"-L", "/usr/local/lib",
/* OpenCV的lib库 */
"/usr/local/lib/libopencv_*",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
3、配置c_cpp_properties.json
在.vscode文件夹下新建一个文件,取名为c_cpp_properties.json
写入以下代码:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
保存后,打开test.cpp,按F5,即可调试
结果图
ps:记得设置断点哟
反思
之前配置vsode环境都是跟着保姆教程来,也没搞懂究竟做了些啥,导致每次配置都会浪费很多时间。还是得抽空把vscode的基本配置知识,一些基本参数熟悉一下。