vscode debug c++与opencv程序
一.工程代碼
1.本文調試代碼文件夾截圖為:
2.代碼細節:
a.h:
#ifndef A_H_ #define A_H_class A {public:A(){}~A(){}void readImg(const char* path);};#endifa.cpp:
#include<opencv2/opencv.hpp> #include <iostream> #include "a.h"using namespace std; void A::readImg(const char* path) {cv::Mat img = cv::imread(path);cout<<"==A img.cols=="<<img.cols<<endl;cout<< "==A img.rows=="<<img.rows<<endl;}main.cpp
#include<opencv2/opencv.hpp> #include<iostream> #include<string> #include"a.h" using namespace std; int main(){cout<<"CV_VERSION"<<CV_VERSION<<endl;const char* imgPath = "/home/fzh/AI/C_learn/datastruct/opencv/test.jpg";cv::Mat img = cv::imread(imgPath);cout<<"==main img.cols=="<<img.cols<<endl;cout<< "==main img.rows=="<<img.rows<<endl; int a = 1;int b = 1;int c; c = a + b;cout<<"=====hahhahhahhah===="<<endl;cout<<"===c=:=="<<c<<endl;A *A1 = new A();A1->readImg(imgPath);delete A1;A1 = nullptr;return 0; }二.開始調試
1.給vs code安裝C/C++插件.
2.按F5開始調試會自動生成.vscode文件夾,launch.json和tasks.json
其中tasks.json作用是執行編譯,launch.json作用是執行編譯后的文件
2.1其中tasks.json內容:
{// See https://go.microsoft.com/fwlink/?LinkId=733558// for the documentation about the tasks.json format//作用:執行編譯"version": "2.0.0","tasks": [{"label": "g++ build active file",//任務名稱 注意要與launch.json文件的"preLaunchTask"關鍵字一致"type": "shell","command": "g++",//終端命令 為g++ //file:當前打開正在編輯的文件名,包括絕對路徑,文件名,文件后綴名//fileDirname:當前打開的文件所在的絕對路徑,不包括文件名//fileBasenameNoExtension:當前打開的文件的文件名,不包括路徑和后綴名"args": [ //終端命令附加參數// "-g", "-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}","-g", "-std=c++11", "${fileDirname}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}", //調試運行多個.cpp文件"-I", "/usr/include","-I", "/usr/include/opencv","-I", "/usr/include/opencv2","-L", "/usr/local/lib","-l", "opencv_core","-l", "opencv_imgproc","-l", "opencv_video","-l", "opencv_ml","-l", "opencv_highgui","-l", "opencv_objdetect","-l", "opencv_flann","-l", "opencv_photo"],"group": {"kind": "build","isDefault": true},"presentation": {"reveal": "silent"},"problemMatcher": "$msCompile"}] }${file}:當前打開正在編輯的文件名,包括絕對路徑,文件名,文件后綴名
${fileDirname}:當前打開的文件所在的絕對路徑,不包括文件名
${fileBasenameNoExtension}:當前打開的文件的文件名,不包括路徑和后綴名
${workspaceFolder}:當前打開的文件夾的絕對路徑
注意:
label為任務名稱,要與launch.json文件的"preLaunchTask"關鍵字一致,
command為終端命令:為g++,
type為shell,
args: 終端命令附加參數,對于單個文件調試 "-g", "-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", “-I”是頭文件所在路徑,注意安裝的opencv路徑,這樣才能#include<opencv2/opencv.hpp>,“-L”是庫文件所在路徑,“-l” 是庫的名字。
多文件調試:?"-g", "-std=c++11", "${fileDirname}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}", //調試運行多個.cpp文件
2.2launch.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": "g++ - Build and debug active file","type": "cppdbg","request": "launch",// "program": "${workspaceFolder}/opencv/main",//workspaceFolder:當前打開的文件夾的絕對路徑//fileDirname:當前打開的文件所在的絕對路徑,不包括文件名//fileBasenameNoExtension:當前打開的文件的文件名,不包括路徑和后綴名"program": "${fileDirname}/${fileBasenameNoExtension}",// "program": "${workspaceFolder}/opencv/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,//用外部終端嗎?為false的話就用vscode終端"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "g++ build active file",//預編譯任務名稱 注意與tasks.json中的"label"關鍵字要一樣"miDebuggerPath": "/usr/bin/gdb" //調試gdb路徑}] }注意:
preLaunchTask為預編譯任務名稱 注意與tasks.json中的"label"關鍵字要一樣,其他的就不用動了,看上面也有了注釋。
上面兩個.json配置好以后就可以調試普通的c++程序;
2.3調試opencv需要配置c_cpp_properties.json
ctrl+shift+p選中這個即可生成c_cpp_properties.json
c_cpp_properties.json內容:
就在生成的基礎上添加includePath的opencv路徑
//作用:添加一些頭文件 {"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**","/usr/include/opencv2","/usr/include/opencv","/usr/include"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "gnu11","cppStandard": "c++17","intelliSenseMode": "clang-x64"}],"version": 4 }然后就可以調試main.cpp了。
總結
以上是生活随笔為你收集整理的vscode debug c++与opencv程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法笔试题
- 下一篇: QT的QStackedLayout