Thursday, January 25, 2024

[SOLVED] OPENCV Compile Error "undefined reference"

Issue

I have a problem to get openCV running on Linux VS Code. I followed the instruction steps, but still fails when compiled on:

/tmp/ccEkxgny.o: In function `cv::String::~String()':
cam_usb.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'

The test code is simple:

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;

int main()
{
    cout << "Test" << endl;
}

Whenever the opencv header is removed, the code executes fine. The c_cpp_properties.json include directory is set up as described in the tutorials, pointing to the correct installation directory:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
**                "/home/XXXX/opencv/include/opencv2/**",**
                "${workspaceFolder}/**",
                "/usr/local/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

I have also linked the dependencies in the tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
**                "-lopencv_core",
                "-lopencv_imgproc",
                "-lopencv_highgui"**
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Still, the error is present. Unfortunately I can not find any further resolutions proposals, any help is highly appreciated. Thanks in advance. (Sorry 1st time user on Linux cpp opencv.. :()


Solution

Thanks to Dan Masek for highlighting the issue of the old OpenCV Version (3.2), I upgraded to OpenCV4 and installed it to the standard /usr/local/... folder. Thanks to fcc for verifying it works on his setup.

I still encountered some issues after the installation of OpenCV4, so, for anybody having problems on VS Code Linux with additional libs, following hint:

In addition to above, the tasks.json file also needed further amendment: extend args with the location of the library - "-I/usr/local/include/opencv4" (Note: new location of library after upgrade)

Complete tasks.json:

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: gcc build active file",
        "command": "/usr/bin/gcc",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}",
            "-lopencv_core",
            "-lopencv_imgproc",
            "-lopencv_highgui",
            "-lopencv_videoio",
            "-I/usr/local/include/opencv4"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"

Works like a charme now - Thanks!



Answered By - TobsterJ
Answer Checked By - Willingham (WPSolving Volunteer)