6

i'm trying to use my webcam using C++ and OpenCV, but i'm gettin this error

(...):Images.cpp:(.text+0x27): undefined reference to cv::VideoCapture::VideoCapture(int)

(...):Images.cpp:(.text+0x38): undefined reference to cv::VideoCapture::~VideoCapture()

(...):Images.cpp:(.text$_ZN2cv6StringD1Ev[_ZN2cv6StringD1Ev]+0x11): undefined reference to cv::String::deallocate() ...

My code:

#include <iostream>
#include <string.h>
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;


int main(int argc, char const *argv[])
{
    VideoCapture cap(0);
    return 0;
}
Community
  • 1
  • 1
Felipe Machado
  • 69
  • 1
  • 1
  • 4

4 Answers4

14

I faced the same issue with respect to opencv version 3.4.3.

With reference to this example, I figured out that I was not including the libopencv_videoio. Once I added it to the project, the build was successful.

In opencv version 3, I think we also need to include the library -lopencv_videoio to use VideoCapture.

Adil B
  • 14,635
  • 11
  • 60
  • 78
  • 1
    A wierd thing. "g++ -Wall -o test2 `pkg-config --libs opencv` test2.cpp" did not work after checking opencv.pc had opencv_videoio in it, which it did. However "g++ -Wall -o test2 test.cpp `pkg-config --libs opencv`" did work. – Gavin Simpson Feb 12 '20 at 08:49
4

The reason your code is not compiling is you are not giving the path of the libraries and the header files. Try compiling the code using the following command.

g++ main.cpp -o main -I <path to opencv header files> -L <path to opencv libraries> -l<name of libraries>

A sample example if you are using linux is

g++ main.cpp -o main -I /usr/local/include -L /usr/local/lib -lopencv_core -lopencv_highgui

Here I am assuming your header file are in /usr/local/include and libraries are in /usr/local/lib and lopencv_core is the name of your library

Lakshya Kejriwal
  • 1,340
  • 4
  • 17
  • 27
2

I had the same problem after forking a Github project. I resolved it using these posts.
My problem was in the Makefile I forked, I had to replace these lines. oldLine:

$(EXECUTABLE): $(OBJECTS)
        $(CC) $(LDFLAGS) $(OBJECTS) -o $@

newLine:

$(EXECUTABLE): $(OBJECTS)
        $(CC) $(OBJECTS) $(LDFLAGS) -o $@

I hope it can help someone ;)

Til
  • 5,150
  • 13
  • 26
  • 34
florian
  • 21
  • 3
1

you probably missing opencv libs , try to add them to your project config file like

LIBS += -LC:\\opencv\\opeencv4.1\\x86\\mingw\\bin \
    libopencv_core410 \
    libopencv_highgui410 \
    libopencv_imgcodecs410 \
    libopencv_imgproc410 \
    libopencv_features2d410 \
    libopencv_video410 \
    libopencv_videoio410 \
user889030
  • 4,353
  • 3
  • 48
  • 51