VSCode debugging C + + program, opencv library and Qt5 Library under Ubuntu

1. Readme

I have been using vscode for more than three years. Most of them write python programs and occasionally C + + programs, but they are not complex and do not involve other third-party libraries. Even if third-party libraries are involved, most of them write CMakeLists files first, and then compile and run it with the standard cmake... make process. But today, I suddenly want to use the debugging function of vscode. The program is a previously written C + + program, involving opencv and Qt library, and then various configurations and attempts. There have been a variety of inexplicable error prompts, but it has been solved in the end, so I recorded it. First, I can quickly find it if I forget it in the future, Second, let the students who are still tossing about C + + debugging on vscode give some tips:

2. Configure three json files

2.1 launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "test",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

There are three points to pay attention to in this file. First: "program": "${workspaceRoot}/test", which indicates the path and name of the file generated after the program is compiled and built (the executable file name here is test); Second: "preLaunchTask": "test". This parameter is the executable program when you F5 debug and run. The name should be the same as "program"; Third: the generated file name must follow task The name of the "label" keyword in JSON is the same, and it is better to be the same as the name of the executable generated by the "args" keyword, so as to avoid all kinds of errors.

2.2 task.json

Note the "label" keyword and the executable name "test" generated by the g + + command - o.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++11",
                "${workspaceRoot}/src/*.cpp",
                "-o",
                "test",
                "-I",
                "/usr/local/include",
                "-I",
                "${workspaceRoot}/include",
                "-I",
                "/usr/local/include/opencv4",
                "-I",
                "/usr/local/include/opencv4/opencv2",
                "-I",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include",
                "-L",
                "/usr/local/lib",
                "-L",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/lib",
                "-l",
                "opencv_aruco",
                "-l",
                "opencv_bgsegm",
                "-l",
                "opencv_bioinspired",
                "-l",
                "opencv_calib3d",
                "-l",
                "opencv_ccalib",
                "-l",
                "opencv_core",
                "-l",
                "opencv_datasets",
                "-l",
                "opencv_dnn_objdetect",
                "-l",
                "opencv_dnn",
                "-l",
                "opencv_dpm",
                "-l",
                "opencv_face",
                "-l",
                "opencv_features2d",
                "-l",
                "opencv_flann",
                "-l",
                "opencv_freetype",
                "-l",
                "opencv_fuzzy",
                "-l",
                "opencv_hfs",
                "-l",
                "opencv_highgui",
                "-l",
                "opencv_imgcodecs",
                "-l",
                "opencv_img_hash",
                "-l",
                "opencv_imgproc",
                "-l",
                "opencv_line_descriptor",
                "-l",
                "opencv_ml",
                "-l",
                "opencv_objdetect",
                "-l",
                "opencv_optflow",
                "-l",
                "opencv_phase_unwrapping",
                "-l",
                "opencv_photo",
                "-l",
                "opencv_plot",
                "-l",
                "opencv_reg",
                "-l",
                "opencv_rgbd",
                "-l",
                "opencv_saliency",
                "-l",
                "opencv_shape",
                "-l",
                "opencv_stereo",
                "-l",
                "opencv_stitching",
                "-l",
                "opencv_structured_light",
                "-l",
                "opencv_superres",
                "-l",
                "opencv_surface_matching",
                "-l",
                "opencv_text",
                "-l",
                "opencv_tracking",
                "-l",
                "opencv_videoio",
                "-l",
                "opencv_video",
                "-l",
                "opencv_videostab",
                "-l",
                "opencv_xfeatures2d",
                "-l",
                "opencv_ximgproc",
                "-l",
                "opencv_xobjdetect",
                "-l",
                "opencv_xphoto",
                "-l",
                "icui18n",
                "-l",
                "icuuc",
                "-l",
                "icudata"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

2.3 c_cpp_properties.json

This file is used to configure the early directory of the header file of the third-party library. Here I use opencv and Qt5

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4/opencv2",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtCore",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtGui",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtQml",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtQuick",
                "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtWidgets"
            ],
            "browse": {
                "path": [
                    "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include",
                    "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtCore",
                    "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtGui",
                    "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtQml",
                    "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtQuick",
                    "/home/jiaken2660/Qt5.9.9/5.9.9/gcc_64/include/QtWidgets",
                "${workspaceRoot}"
                ]
            },
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "c++11",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

3. Common errors

3.1 Opencv error

It is assumed that there is no problem with OpenCV installation, because I have no problem generating programs with cmake and make. The main reason here is that there is no correct link to opencv library. Please see task The configuration mode of JSON here is a little different from that of writing CMakeLists file. lib is not required in the front and neither is required in the back so end.

undefined reference to `cv::imread(std::string const&, int)'
undefined reference to `cv::noArray()'
undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
undefined reference to `cv::waitKey(int)'

3.2Qt error

I can't understand the prompt here. At first glance, it's libqt5core so. 5. The link is wrong, so I never found a solution. Later, I wanted to solve the warning first, so I put libicui18n so. 56,libicuuc.so.56 and libicudata so. 56 is added to task as a link library JSON file, the problem is solved.

/usr/bin/ld: warning: libicui18n.so.56, needed by //opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libicuuc.so.56, needed by //opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libicudata.so.56, needed by //opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'U'_ strToLower_ 56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'ucnv_getStandardName_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'ucnv_getAlias_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'uenum_next_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'U'_ strToUpper_ 56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'ucnv_setSubstChars_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'ucal_getTimeZoneDisplayName_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'ucnv_fromUnicode_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'u_errorName_56 'undefined reference
//opt/Qt5.10.0/5.10.0/gcc_64/lib/libQt5Core.so.5: Right 'uenum_close_56 'undefined reference 
...

3.3 other errors

If there are no other errors, please see the configuration of the three files in Section 1. In particular, the file names must be consistent.

Keywords: C++ Linux Machine Learning

Added by mlschutz on Wed, 09 Feb 2022 12:15:01 +0200