简体   繁体   中英

cv::VideoCapture from string gives linker error in Qt5 on Mac

Problem

I am trying to get the OpenCV VideoCapture class running in a Qt project.

When I call it with an int (0 for the video camera on my mac), it works fine:

#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
...
cv::VideoCapture cap(0);

The project compiles and when I run it, the light on my camera goes on.

BUT: when I try to compile it with a std::string as argument, I get a linker error:

#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
...
cv::VideoCapture cap("/Users/xxxxx/small.mp4");

results in:

Undefined symbols for architecture x86_64:
  "cv::VideoCapture::VideoCapture(std::string const&)", referenced from:
  MainWindow::MainWindow(QWidget*) in mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

althought the constructor from std::string should actually exist: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-videocapture

Settings

My specifications:

  • Qt 5.4.0 (Clang 6.0 (Apple), 64 bit)
  • Mac OSX 10.10.1
  • OpenCV 2.4.9 installed from homebrew

My .pro file:

QT       += core gui widgets

TARGET = VideoCaptureTest
TEMPLATE = app

SOURCES += main.cpp mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += /usr/local/include

LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_videostab
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann

What I tried

1.

I found this question on SO, which seems to be the exact same problem as mine: Linking error: undefined reference to `cv::VideoCapture::open(std::string const&)`

It seem my problem could have something to do with clashing versions of OpenCV. But I checked my include directories and my lib directories and I seem to only have 2.4.9 installed. Is there maybe a different version that comes with Qt?

I tried including version 2.4.9 directly in my .pro file with

LIBS += -lopencv_core2.4.9
...

But how can I make sure I include this version as well in my source file?

2.

I though it might have something to do with Mac and x86_64 stuff. I included the following lines, which I found somewhere online, in my .pro file:

CONFIG += x86 ppc x86_64 ppc64
CONFIG += MAC_CONFIG

MAC_CONFIG {
     QMAKE_CXXFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.7
     QMAKE_LFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.7
}

But this also showed no effect.

3.

Here ( http://answers.opencv.org/question/14772/solved-linking-error-undefined-reference-to-cvvideocaptureopenstdstring-const/ ) it is suggested to try cv::String instead of std::string . I also tried this:

#include "opencv2/opencv.hpp"
...
cv::String s("/Users/xxxxx/small.mp4");
cv::VideoCapture cap(s);

and strangely I still get the exact same error:

Undefined symbols for architecture x86_64:
  "cv::VideoCapture::VideoCapture(std::string const&)", referenced from:
  MainWindow::MainWindow(QWidget*) in mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

which seems like the cv::String is actually converted to std::string , I guess because the compiler somewhere finds the definition of the constructor with std::string , but the linker does not?

4.

Apparently there was a similar problem with OSX 10.9, not with cv::VideoCapture, but in general resulting also in linking error for x86_64. The solution was to include

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9

in the .pro file. I tried this as well with 10.9 and 10.10 (which I am actually using). Still the same error. Could this be a problem with my version of OSX?

Help

I found no other suggestions on how to solve the problem. Does somebody have an idea what I am doing wrong? Any help appreciated!

Patrick

The answer has been given in the discussion(@wezside) of the link (item 1 of your description).

for OpenCV 3.0 (also 2.4.9 ?)

you need to add

LIBS += -lopencv_videoio

to the .pro, which is missing in yours.

After researching for several hours, I found that adding QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.11 to your .pro file and removing the previous .pro.user file and build directory can solve the problem. The second step is required.

I'm on El Capitan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM