Undefined Reference to cv::

asked 2017-09-21 15:06:54 -0600

Intoxica gravatar image

updated 2017-09-22 11:53:05 -0600

I am getting undefined references to cv::. New to opencv and opensource environments. Using openv 3.3 on Ubuntu 16.04. Installed Opencv using the tutorial on opencv.org except have not set the cmake parameters; instead just ran cmake .. ; I understand something related to linking library or in the cmakelists.txt but I am unsure of what exactly needs to be done. If someone could help with the steps, it would be great. Thank you.

The error message is:

/tmp/ccP0Zecd.o: In function main': boxdetect.cpp:(.text+0x91): undefined reference tocv::imread(cv::String const&, int)' boxdetect.cpp:(.text+0xb1): undefined reference to cv::operator==(cv::Mat const&, cv::Mat const&)' boxdetect.cpp:(.text+0x198): undefined reference tocv::threshold(cv::_InputArray const&, cv::_OutputArray const&, double, double, int)' boxdetect.cpp:(.text+0x1fc): undefined reference to cv::imshow(cv::String const&, cv::_InputArray const&)' boxdetect.cpp:(.text+0x2c4): undefined reference tocv::findContours(cv::_InputOutputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, int, int, cv::Point_<int>)' boxdetect.cpp:(.text+0x49b): undefined reference to cv::contourArea(cv::_InputArray const&, bool)' boxdetect.cpp:(.text+0x5af): undefined reference tocv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)' boxdetect.cpp:(.text+0x693): undefined reference to cv::drawContours(cv::_InputOutputArray const&, cv::_InputArray const&, int, cv::Scalar_<double> const&, int, int, cv::_InputArray const&, int, cv::Point_<int>)' boxdetect.cpp:(.text+0x7bf): undefined reference tocv::imshow(cv::String const&, cv::_InputArray const&)' boxdetect.cpp:(.text+0x7e7): undefined reference to cv::waitKey(int)' /tmp/ccP0Zecd.o: In functioncv::String::String(char const*)': boxdetect.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x54): undefined reference to cv::String::allocate(unsigned long)' /tmp/ccP0Zecd.o: In functioncv::String::~String()': boxdetect.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to cv::String::deallocate()' /tmp/ccP0Zecd.o: In functioncv::String::operator=(cv::String const&)': boxdetect.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to cv::String::deallocate()' /tmp/ccP0Zecd.o: In functioncv::Mat::~Mat()': boxdetect.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to cv::fastFree(void*)' /tmp/ccP0Zecd.o: In functioncv::Mat::release()': boxdetect.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()' collect2: error: ld returned 1 exit status

This is the example from the Opencv book I am trying to run for which i get the above error:

#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <algorithm>
#include <iostream>

using namespace std;

struct AreaCmp 
    AreaCmp(const vector<float>& _areas) : areas(&_areas) {}
    bool operator()(int a, int b) const {return (*areas)[a] > (*areas)[b];}
    const vector<float>* areas;

int main(int argc, char* argv[])
cv::Mat img, img_edge,img_color;
img == cv :: imread(argv[1],cv::IMREAD_GRAYSCALE);

if(argc!=2 || (img.empty()))
    cout<< "Check file and file format";
    return -1;

cv::imshow("Image after threshold", img_edge);
vector<vector<cv::Point> >contours;


cout<< "\n\nHit any key to ...
edit retag flag offensive close merge delete


imread is imgcodecs lib imshow in highgui drawcontour in imgproc and you will need imgcore too

Now my system is windows But I think all libs start with libopencv_

LBerger gravatar imageLBerger ( 2017-09-21 15:42:03 -0600 )edit

Thanks, I added the code that gives the error, also tried and checked after adding imgcodec.hpp. Still the same error.

Intoxica gravatar imageIntoxica ( 2017-09-22 12:02:09 -0600 )edit

No it's a link error your code is correct. Try to download this compress folder. unzip it and in terminal cd infolderunncompress and ./buildmysource.sh and I hope it will be good

PS if you use #include "opencv2/opencv.hpp" you don''t other includes

LBerger gravatar imageLBerger ( 2017-09-22 12:30:19 -0600 )edit

I tried doing that but got "Permission denied " to sun the .sh file. Then I created a new directory with build and the CMakelists.txt and ran cmake and cmake build inside the build directory. But still the same error.

Intoxica gravatar imageIntoxica ( 2017-09-22 13:29:57 -0600 )edit

Have you got two opencv version? How do you install opencv?

LBerger gravatar imageLBerger ( 2017-09-22 13:52:47 -0600 )edit

I cloned from github and installed using cmake (using instructions on opencv.org). As I was getting this error, I tried to uninstall and reinstall, I think there might be two versions if i didnt uninstall properly.

Intoxica gravatar imageIntoxica ( 2017-09-22 13:56:32 -0600 )edit

You must have admin right to install opencv: sudo make install. If you cannot install opencv try opencv in static. Using windows you can use opencv without an installation process but I don't know linux

LBerger gravatar imageLBerger ( 2017-09-22 14:01:24 -0600 )edit