vscode debugging c++ and opencv programs

1. Engineering code

1. The screenshot of the debugging code folder in this article is:

2. Code details:

a.h:

#ifndef A_H_
#define A_H_
 
class A
{
  public:
	A(){}
	~A(){}
 
	void readImg(const char* path);
    
};
 
#endif

a.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;
}   

2. Start debugging

1. Install the C/C++ plugin for vs code.

2. Press F5 to start debugging, the .vscode folder, launch.json and tasks.json will be automatically generated

The role of tasks.json is to execute compilation, and the role of launch.json is to execute the compiled file

2.1 The contents of 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}: The name of the currently opened file being edited, including the absolute path, file name, and file suffix
${fileDirname}: The absolute path of the currently opened file, excluding the file name
${fileBasenameNoExtension}: The currently opened file The file name, excluding the path and suffix
${workspaceFolder}: the absolute path of the currently opened folder

note:

label is the task name, which should be consistent with the "preLaunchTask" keyword in the launch.json file

command is terminal command: g++

args: additional parameters for terminal commands, for single file debugging "-g", "-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", "-I" is the path where the header file is located, pay attention to the installed opencv path, so as to #include<opencv2/opencv.hpp>, "-L" is the path where the library file is located, and "-l" is the name of the library.

Multi-file debugging: "-g", "-std=c++11", "${fileDirname}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}", //Debug Run multiple .cpp files

2.2launch.json content is as follows:

{
    // 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路径
        }
    ]
}

note:

preLaunchTask is the name of the pre-compiled task. Note that it is the same as the "label" keyword in tasks.json. You don't need to move the others. See the comments above.

After the above two .json are configured, ordinary C++ programs can be debugged;

2.3 To debug opencv, you need to configure c_cpp_properties.json

ctrl+shift+p select this to generate c_cpp_properties.json

c_cpp_properties.json content:

Just add the opencv path of includePath on the basis of the generation

//作用:添加一些头文件
{
    "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
}

Then you can debug main.cpp.

Guess you like

Origin blog.csdn.net/fanzonghao/article/details/115280826
Recommended