0

I am on Ubuntu and want to install a different version of OpenCV(2.4.13) on a custom directory. I followed this tutorial here: http://code.litomisky.com/2014/03/09/how-to-have-multiple-versions-of-the-same-library-side-by-side/

I cannot get this simple main.cpp program to compile. I cannot create a cv::Mat image, but I can obtain the OpenCV version just fine!:

#include <iostream>
#include <opencv2/core/version.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main(int argc, char ** argv)
{
std::cout << "OpenCV version: "
        << CV_MAJOR_VERSION << "." 
        << CV_MINOR_VERSION << "."
        << CV_SUBMINOR_VERSION
        << std::endl;

cv::Mat image; //without this line, it works!


return 0;
}

Here is my makefile:

CPP = g++ -std=c++0x

# OpenCV 2.4.13
CPPFLAGS = -L/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/lib  \
           -I/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/include


all: test

test: main.cpp
     $(CPP) $(CPPFLAGS) $^ -o $@

This is the compiler error:

/tmp/ccyrdd7H.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccyrdd7H.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [test] Error 1
user1431515
  • 185
  • 1
  • 10

2 Answers2

1

Your Makefile is wrong. The -L & -l options are relevant at link time (but not at compile time). See this & that answers and examples (both quite similar to your question, and showing a Makefile that you should be able to adapt). Run make -p to understand the builtin rules and variables used by them.

(I am guessing you are on Linux)

Read more about ELF, linkers & dynamic linkers, object files, executables, etc.. See Levine's Linkers and loaders book.

BTW, CV_MAJOR_VERSION etc... is probably a macro defined in some header (mentioned by some of your #include directives). But cv::Mat is probably some genuine C++ class, with a constructor or a vtable which is referenced at link time.

Perhaps you should also read a good book about C++, e.g. Programming -- Principles and Practice Using C++; but we don't have time and space to explain all that here.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

The makefile should be as so:

CPP = g++ -std=c++0x

# OpenCV trunk
CPPFLAGS = -L/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/lib -lopencv_core -lopencv_highgui -lopencv_imgproc  \
           -I/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/include

all: test

test: main.cpp
    $(CPP) $(CPPFLAGS) $^ -o $@

The flags -lopencv_core -lopencv_highgui -lopencv_imgproc were missing.

With installation of opencv in a different directory, you may come up with a shared library error that cannot be accessed. In such cases follow the instructions here: openCV program compile error "libopencv_core.so.2.4: cannot open shared object file: No such file or directory" in ubuntu 12.04

Community
  • 1
  • 1
user1431515
  • 185
  • 1
  • 10