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

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.

Related

compilation error of OpenCV cpp code using Qt

I am using a very basic OpenCV code to compile using Qt but every time the following errors are coming.
OpenCV version
OpenCV version : 3.4.3
Major version : 3
Minor version : 4
Subminor version : 3
My code
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat I = imread("path/filename.ppm", 1);
imshow("conv_image",I);
waitKey();
return 0;
}
.pro file
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you
use
# any feature of Qt which as been marked deprecated (the exact
warnings
# depend on your compiler). Please consult the documentation of
the
# deprecated API in order to know how to port your code away from
it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use
deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a
certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables
all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -
lopencv_highgui
Given Error
:-1: warning: libopencv_core.so.3.4, needed by /usr/local/lib/libopencv_imgcodecs.so, may conflict with libopencv_core.so.2.4
:-1: error: main.o: undefined reference to symbol '_ZN2cv6String10deallocateEv'
/usr/local/lib/libopencv_core.so.3.4:-1: error: error adding symbols: DSO missing from command line
:-1: error: collect2: error: ld returned 1 exit status
In my .pro file, I have used the following line(from the different link I have found 3 ways, I have tried all but failed)
First
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -
lopencv_highgui
Second
INCLUDEPATH +=/usr/local/include/opencv
LIBS +=`pkg-config --libs opencv`
Third
I have used information from here
Please give me suggestion to solve the issue.

Undefined reference to symbol 'gzclose'

I guess this is a linker error, but I've spent a lot of time and haven't find a working answer. I have an OpenCV C++ program, when I try to compile something of the style cv::Mat newMatObject; it gives me the error /usr/local/lib/libopencv_core.a(persistence.cpp.o): undefined reference to symbol 'gzclose'.
The .pro file contains the following lines:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtTest
TEMPLATE = app
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lz -ltbb -ltiff
SOURCES += main.cpp\
qttest.cpp
HEADERS += qttest.h
FORMS += qttest.ui
Am I forgetting something/doing something wrong?
Some Info:
Kubuntu 16.04 LTS 64 Bits
Qt Creator 4.0.2, based on Qt 5.7.0
OpenCV 2.4.13
Addendum:
I had the program working properly with OpenCV 2.4.9, no other changes. I decided to move on to 2.4.13 because of the extended gui, which didn't work on 2.4.9. I downloaded OpenCV 2.4.13 from the official website, and installed it with this script (except for the wget and unzip, which were made manually), initially didn't autoremove the previous library, but I did after the issue came up and rerun the previous script again, to ensure proper installation.
Edit 1:
Running
g++ -o opencvtest opencvtest.cpp `pkg-config opencv --cflags --libs`
with similar lines of code provides the desired result without a problem.
Add -lz to your LIBS:
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lz

Qt Creator cannot find openCV libraries

First of all I know this question has been asked (see here) but those solutions did not work for me.
I am trying to install opencv to use with Qt Creator on ubuntu 14.04.
I followed this tutorial exactly and confirmed that it worked by running the facedetect example.
I then started on this tutorial for using Qt creator with opencv. I followed the steps in the video exactly, however when I build and run the example I get the following errors:
/usr/bin/ld: cannot find -lopencv_core
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_highgui
full compile output:
g++ -Wl,-rpath,/home/tpst/Program_Files/Qt-5.3.1/5.3/gcc_64 -Wl,-rpath,/home/tpst/Program_Files/Qt-5.3.1/5.3/gcc_64/lib -o test main.o -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -L/home/tpst/Program_Files/Qt-5.3.1/5.3/gcc_64/lib -lQt5Core -lpthread
/usr/bin/ld: cannot find -lopencv_core
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_highgui
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
23:02:50: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project test (kit: Desktop Qt 5.3 GCC 64bit)
When executing step 'Make'
And here is the contents of my .pro file:
QT += core
QT -= gui
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui
SOURCES += main.cpp
I have also tried to compile using terminal and get the same (/usr/bin/ld: cannot find -llib)
I dont understand why Qt cant find the libraries. You can see that they exist in the directory
Some help would be appreciated. I'm new to linux and Qt, I dont really understand the solutions posted to similar questions and have not been able to solve this myself.
afaik your linked libraries must look like libName.so but your libraries are named libName.so.version so the linker doesn't find them. Try to create symbolic links:
ln /usr/local/lib/libopencv_core.so.3.0.0 /usr/local/lib/libopencv_core.so
ln /usr/local/lib/libopencv_imgcodecs.so.3.0.0 /usr/local/lib/libopencv_imgcodecs.so
ln /usr/local/lib/libopencv_highgui.so.3.0.0 /usr/local/lib/libopencv_highgui.so

OpenCv + mac os x + qt creator = strange linking bug?

I'm trying to build simple OpenCV app using qt creator on Mac Os X 10.9. So i've installed OpenCv and Qt4 using brew:
brew install opencv
brew install qt4
and then downloaded qt creator. Than i've made simple project - main.cpp:
#include <QCoreApplication>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mat m;
m = Mat(2, 3, 4);
cv::threshold(m, m, 123, 200, 1);
m = cv::imread("asdasd", 1); //problem is here!
return a.exec();
}
and .pro file:
QT += core
QT -= gui
TARGET = opencv_test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
LIBS += -L/usr/local/lib \
-lopencv_core \
-lopencv_imgproc \
-lopencv_highgui \
-lopencv_objdetect \
-lopencv_calib3d
INCLUDEPATH += /usr/local/include/opencv
DEPENDPATH += /usr/local/include/opencv/include
INCLUDEPATH += $$PWD/../../../usr/local/include
DEPENDPATH += $$PWD/../../../usr/local/include
DEPENDPATH += $$PWD/../../../usr/local/lib
Now the weird part - if i comment this line: m = cv::imread("asdasd", 1); //problem is here! and build project, everything is fine - it will compile and link without any problem. So it seems that .dylib files are ok, because i can use OpenCV functions - Mat object constructor and threshold function. But! If i don't comment line with imread i get this error: :-1: błąd:symbol(s) not found for architecture x86_64 or to be more precise:
Undefined symbols for architecture x86_64: "cv::imread(std::string
const&, int)", referenced from:
_main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see
invocation)
Generally the problem is just a part of bigger problem - i'm trying to build much bigger project using qt creator, but linker can't find some(but not all!) functions. In project which uses lot of OpenCV functions linker has got problems only with:
cv::VideoCapture::VideoCapture(std::string const&)
cv::destroyWindow(std::string const&)
cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)
cv::CascadeClassifier::CascadeClassifier(std::string const&)
cv::imshow(std::string const&, cv::_InputArray const&)
In this project i'm using much more functions (mostly basic image processing - threshold, image copying, drawing basic primitives, finding contours) and linker can handle them without any problem.
What i've already tried - building OpenCv and Qt4(only using build brew install qt4 --from-source) from sources. I've builded OpenCv using this tutorial - http://sadeepj.blogspot.com/2012/03/installing-and-configuring-opencv-to.html I've also tried a lot of CMAKE options - -DCMAKE_OSX_ARCHITECTURES=x86_64 or i386 and -DCMAKE_CXX_FLAGS="-stdlib=libc++". I've also tried to build it using gcc(defautt compiler is clang) version 4.2, but it failed(building failed).
i've tried with OpenCV 2.4.8 and 2.4.2.
Also i've tried to set:
CMAKE_CXX_FLAGS += -std=c++11
CMAKE_CXX_FLAGS += -stdlib=stdlibc++
Still no luck.
I've checked whether builded .dylib files are x86_64:
/usr/local/lib[master]$ lipo -info libopencv_core.dylib Non-fat file:
libopencv_core.dylib is architecture: x86_64
and whether highgui file contains any information about imread function(i know that it is not a proof that it export this function):
/usr/local/lib[master]$ nm libopencv_highgui.dylib | grep imread
00000000000069b0 T __ZN2cv6imreadERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEi
0000000000006ac0 t __ZN2cvL7imread_ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEiiPNS_3MatE
Information which might be important: Mac OS X 10.9, 64 bit.
/usr/local/lib[master]$ clang --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
/usr/local/lib[master]$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
Btw - isn't it strange that running gcc --version in fact runs clang ?
So the question is - what can i do to compile and build it? Should i try using gcc or maybe something else will solve this problem? Also - if someone could upload his libraries and share a link it, so i could try with them it would be great. Any solution that will solve this problem will be fine for me - i can build everything from source, use any compiler, linker, etc.. - just help me make it working and i will be happy :)
Ok - finally after 4 days of searching i've solved this problem. Generally solution is simple - just add:
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
to you .pro file. If don't want to add it to every .pro file you can set it "globally" - just set it in this file:
Qt5.2.0/5.2.0-rc1/clang_64/mkspecs/macx-clang/qmake.conf
More information about this bug/problem:
Qt5.1/Qt5.2 + Mac OS 10.9 (Mavericks) + XCode 5.0.2, Undefined symbols for architecture x86_64
http://qt-project.org/forums/viewthread/35646/ <-- mostly here
//edit:
This solution only works with CLang, no luck with gcc.

Include System Library with qmake on osx

i have a working cmake project that i need to move to qmake.
Everything is working but the include of a library called octomap.
I installed ( sudo make install ) octomap in system
/usr/local/lib/liboctomap.a
/usr/local/lib/liboctomap.dylib
and the headers in
/usr/local/lib/include/octomap/
Lets say project is so composed:
foo.h
...
#include <octomap/OcTree.h>
octomap::OcTree tree(0.1);
...
foo.cpp
...
#include <octomap/OcTree.h>
using namespace octree;
tree.doSomething();
...
foo.pro
...
unix|win32: LIBS += -loctomap
INCLUDEPATH += /usr/local/include/
...
Compiling results in a error: symbol(s) not found for architecture x86_64 due to
Undefined symbols for architecture x86_64:
"octomap::OcTreeNode::createChild(unsigned int)", referenced from:
octomap::OccupancyOcTreeBase<octomap::OcTreeNode>::updateNodeRe...[a very long stack]
That should means that octomap library has not be linked, and i tried also adding it in the project, using pkgconfig and using an absolute path. They all result in same error, while if i use it in a cmake project it works fine.
It was just a missing library, i also had to include the octomath lib
unix|win32: LIBS += -L/usr/local/lib/ -loctomap
unix|win32: LIBS += -L/usr/local/lib/ -loctomath
hope can be useful to someone.