lordoku
lordoku

Reputation: 1112

How to link to opencv 3.0 when compiled from source on Ubuntu 14.04?

I'm trying to compile and link the following opencv program that I found online:

main.cpp:

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main( int argc, const char** argv )
{
        VideoCapture capture(0);

        while(capture.isOpened() )
        {
                Mat frame;
                if (! capture.read(frame))
                        continue;

                imshow("lalala", frame);
                if(waitKey(10) == 27)
                        break;
        }



        return 0;
}

My filestructure is as follows:

~/dev/opencv-1

~/dev/opencv-example

The opencv-1 is a git clone of: https://github.com/Itseez/opencv

I followed the instructions located at: http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html

My goal was to debug into the opencv code, so I used the CMAKE_BUILD_TYPE=Debug

I was able to get the opencv code to compile sucessfuly. I wrote the following makefile to compile this sample:

INCPATH =-I../opencv-1/modules/objdetect/include \
   -I../opencv-1/modules/highgui/include \
   -I../opencv-1/modules/imgproc/include \
   -I../opencv-1/modules/core/include \
   -I../opencv-1/modules/imgcodecs/include \
   -I../opencv-1/modules/videoio/include \

LIBPATH =-L../opencv-1/debug/lib

LIBS =-lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopencv_video -lopencv_objdetect

%:%.cpp
        g++ $(INCPATH) $(LIBS) $(LIBPATH) $^ -o $@

To compile I ran: make main

When this happens, I get the following linking errors:

g++ -I../opencv-1/modules/objdetect/include -I../opencv-1/modules/highgui/include -I../opencv-1/module                                                              s/imgproc/include -I../opencv-1/modules/core/include -I../opencv-1/modules/imgcodecs/include -I../open                                                              cv-1/modules/videoio/include  -lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopenc                                                              v_video -lopencv_objdetect -L../opencv-1/debug/lib main.cpp -o main
/tmp/cc0PMDdW.o: In function `main':
main.cpp:(.text+0x29): undefined reference to `cv::VideoCapture::VideoCapture(int)'
main.cpp:(.text+0x69): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
main.cpp:(.text+0xce): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
main.cpp:(.text+0xf6): undefined reference to `cv::waitKey(int)'
main.cpp:(.text+0x131): undefined reference to `cv::VideoCapture::isOpened() const'
main.cpp:(.text+0x14d): undefined reference to `cv::VideoCapture::~VideoCapture()'
main.cpp:(.text+0x1b0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/cc0PMDdW.o: In function `cv::String::String(char const*)':
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4f): undefined reference to `cv::String::al                                                              locate(unsigned long)'
/tmp/cc0PMDdW.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallo                                                              cate()'
/tmp/cc0PMDdW.o: In function `cv::_InputArray::_InputArray()':
main.cpp:(.text._ZN2cv11_InputArrayC2Ev[_ZN2cv11_InputArrayC5Ev]+0x13): undefined reference to `vtable                                                               for cv::_InputArray'
/tmp/cc0PMDdW.o: In function `cv::_InputArray::_InputArray(cv::Mat const&)':
main.cpp:(.text._ZN2cv11_InputArrayC2ERKNS_3MatE[_ZN2cv11_InputArrayC5ERKNS_3MatE]+0x17): undefined re                                                              ference to `vtable for cv::_InputArray'
/tmp/cc0PMDdW.o: In function `cv::_InputArray::~_InputArray()':
main.cpp:(.text._ZN2cv11_InputArrayD2Ev[_ZN2cv11_InputArrayD5Ev]+0x13): undefined reference to `vtable                                                               for cv::_InputArray'
/tmp/cc0PMDdW.o: In function `cv::_OutputArray::_OutputArray(cv::Mat&)':
main.cpp:(.text._ZN2cv12_OutputArrayC2ERNS_3MatE[_ZN2cv12_OutputArrayC5ERNS_3MatE]+0x23): undefined re                                                              ference to `vtable for cv::_OutputArray'
/tmp/cc0PMDdW.o: In function `cv::_OutputArray::~_OutputArray()':
main.cpp:(.text._ZN2cv12_OutputArrayD2Ev[_ZN2cv12_OutputArrayD5Ev]+0x13): undefined reference to `vtab                                                              le for cv::_OutputArray'
/tmp/cc0PMDdW.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cc0PMDdW.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::dea                                                              llocate()'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

I'm not sure what I'm missing. I've included all of the libraries that this simple program should need to compile. I've searched online for similar errors and all of the answers I've seen suggest adding the libraries that are already in my makefile.

Upvotes: 0

Views: 3098

Answers (1)

Chnossos
Chnossos

Reputation: 10486

Your problem is that this line:

g++ $(INCPATH) $(LIBS) $(LIBPATH) $^ -o $@

Should be re-written like this:

$(CXX) $(INCPATH) $(LIBPATH) $^ $(LIBS) -o $@

The variable containing -l flags should always go after the .cpp or .o files in the command line.


But you can do better. Your Makefile should ultimately look like this:

CPPFLAGS    :=  -I../opencv-1/modules/objdetect/include \
                -I../opencv-1/modules/highgui/include \
                -I../opencv-1/modules/imgproc/include \
                -I../opencv-1/modules/core/include \
                -I../opencv-1/modules/imgcodecs/include \
                -I../opencv-1/modules/videoio/include

LDFLAGS     :=  -L../opencv-1/debug/lib

LDLIBS      :=  -lopencv_core -lopencv_videoio -lopencv_highgui \
                -lopencv_imgproc -lopencv_video -lopencv_objdetect

Nothing more, nothing less. GNU Make already has an implicit rule to compile a .cpp file into an executable, just rely on it by using the built-in variables.

Note: CPPFLAGS stands for preprocessor flags, not c++ flags. Use CXXFLAGS to provide flags such as -std or -g flags.

Upvotes: 1

Related Questions