kyuunin
kyuunin

Reputation: 51

OpenCV2 Code linkage Error

I try to Compile this Simple Demo for HOG feature detection with openCV in C++ on Ubuntu 16.04 Original Code

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
    VideoCapture cap(CV_CAP_ANY);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);    
    if (!cap.isOpened())
        return -1;

    Mat img;
    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    namedWindow("video capture", CV_WINDOW_AUTOSIZE);
    while (true)
    {
        cap >> img;
        if (!img.data)
            continue;

        vector<Rect> found, found_filtered;
        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);

        size_t i, j;
        for (i=0; i<found.size(); i++)
        {
            Rect r = found[i];
            for (j=0; j<found.size(); j++)
                if (j!=i && (r & found[j])==r)
                    break;
            if (j==found.size())
                found_filtered.push_back(r);
        }
        for (i=0; i<found_filtered.size(); i++)
        {
        Rect r = found_filtered[i];
            r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8);
        r.y += cvRound(r.height*0.06);
        r.height = cvRound(r.height*0.9);
        rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);
    }
        imshow("video capture", img);
        if (waitKey(20) >= 0)
            break;
    }
    return 0;
}

when I tried to compile the code with

g++ -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo  -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab hog.cpp

I get following error

/tmp/cctllC37.o: In function `main':
hog.cpp:(.text+0x38): undefined reference to `cv::VideoCapture::VideoCapture(int)'
hog.cpp:(.text+0x65): undefined reference to `cv::VideoCapture::set(int, double)'
hog.cpp:(.text+0x92): undefined reference to `cv::VideoCapture::set(int, double)'
hog.cpp:(.text+0xa1): undefined reference to `cv::VideoCapture::isOpened() const'
hog.cpp:(.text+0xdc): undefined reference to `cv::HOGDescriptor::getDefaultPeopleDetector()'
hog.cpp:(.text+0x10e): undefined reference to `cv::HOGDescriptor::setSVMDetector(cv::_InputArray const&)'
hog.cpp:(.text+0x15b): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
hog.cpp:(.text+0x17e): undefined reference to `cv::VideoCapture::operator>>(cv::Mat&)'
hog.cpp:(.text+0x23b): undefined reference to `cv::HOGDescriptor::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, cv::Size_<int>, cv::Size_<int>, double, double, bool) const'
hog.cpp:(.text+0x51e): undefined reference to `cv::rectangle(cv::Mat&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)'
hog.cpp:(.text+0x545): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
hog.cpp:(.text+0x588): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
hog.cpp:(.text+0x5b0): undefined reference to `cv::waitKey(int)'
hog.cpp:(.text+0x637): undefined reference to `cv::VideoCapture::~VideoCapture()'
hog.cpp:(.text+0x71f): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/cctllC37.o: In function `cv::Mat::~Mat()':
hog.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cctllC37.o: In function `cv::Mat::release()':
hog.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
/tmp/cctllC37.o: In function `cv::HOGDescriptor::HOGDescriptor()':
hog.cpp:(.text._ZN2cv13HOGDescriptorC2Ev[_ZN2cv13HOGDescriptorC5Ev]+0xd): undefined reference to `vtable for cv::HOGDescriptor'
/tmp/cctllC37.o: In function `cv::HOGDescriptor::~HOGDescriptor()':
hog.cpp:(.text._ZN2cv13HOGDescriptorD2Ev[_ZN2cv13HOGDescriptorD5Ev]+0xd): undefined reference to `vtable for cv::HOGDescriptor'
/tmp/cctllC37.o: In function `cv::_InputArray::_InputArray<float>(std::vector<float, std::allocator<float> > const&)':
hog.cpp:(.text._ZN2cv11_InputArrayC2IfEERKSt6vectorIT_SaIS3_EE[_ZN2cv11_InputArrayC5IfEERKSt6vectorIT_SaIS3_EE]+0x11): undefined reference to `vtable for cv::_InputArray'
collect2: error: ld returned 1 exit status

I tried to use different Compiler Flags, but nothing seemed to work. I have installed all openCV packages I could find in the default Ubuntu repro.

Upvotes: 1

Views: 424

Answers (2)

Kinght 金
Kinght 金

Reputation: 18341

Your link error means the compiler can't find the headers of OpenCV. So you should tell the compiler where is your OpenCV installed. such as:

g++ -std=c++11 test.cpp \
-I/home/xxx/Programs/OpenCV/shared_world/include/opencv \
-I/home/xxx/Programs/OpenCV/shared_world/include \
-L/home/xxx/Programs/OpenCV/shared_world/lib -lopencv_world 

By the way, I compile and install OpenCV by myself with opencv.pc installed and environment setup.

When compile and run OpenCV programs, I just use this line:

g++ -std=c++11 test.cpp `pkg-config --cflags --libs opencv`  -o main && ./main

Upvotes: 1

user4207183
user4207183

Reputation:

Try to use "-Lpath" compiler flag to tell the linker where the libraries are installed if they are not the usual /usr/lib etc...

Be sure that all the libraries exist into the system.

Upvotes: 0

Related Questions