Question

i want to use openCV lib in qt on mac. i read a lot of about how to configure pro file in qt (i have 5.1) to use OpenCV, but nothing works for me. what i done:

1) sudo port install opencv
2) sudo port install pkg-config
3) add to *.pro file: LIBS += `pkg-config --libs opencv`

but i only get error:

    Target: x86_64-apple-darwin13.0.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -headerpad_max_install_names -macosx_version_min 10.9.0 -o opencv_pokus3.app/Contents/MacOS/opencv_pokus3 -L/opt/local/lib -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk main.o mainwindow.o moc_mainwindow.o -lopencv_core -lopencv_highgui -framework QtWidgets -framework QtGui -framework QtCore -framework OpenGL -framework AGL -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a -F/Applications/Qt5.1.1/5.1.1/clang_64/lib
Undefined symbols for architecture x86_64:
  "cv::imread(std::string const&, int)", referenced from:
      _main in main.o
  "std::allocator<char>::allocator()", referenced from:
      _main in main.o
  "std::allocator<char>::~allocator()", referenced from:
      _main in main.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
      _main in main.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", 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)
make: *** [opencv_pokus3.app/Contents/MacOS/opencv_pokus3] Error 1
12:56:15: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project opencv_pokus3 (kit: Desktop Qt 5.1.1 clang 64bit)
When executing step 'Make'
12:56:15: Elapsed time: 00:00.

i try change step 3 to:

LIBS += -L/opt/local/lib \
 -lopencv_calib3d \
 -lopencv_contrib \
 -lopencv_core \
 -lopencv_features2d \
 ...

but, unfortunately it gets same result...

PS: i can compile openCV helloWorld program through terminal :

g++ `pkg-config --libs --cflags opencv` -o helloWorld  helloOpenCV.cpp
or
clang++ `pkg-config --libs --cflags opencv` -o helloWorld  hello.cpp

and everything is ok...

my configuration: OSX 10.9, QT 5.1.1, opencv 2.4.7 install through macports, pkg-config 0.28 install through macports,

simple hello world program:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
...
char var;
cv::Mat img;
img = cv::imread("./imgres.jpg");
cv::namedWindow("Image");
cv::imshow("Image", img);
std::cin >> var;
...

do you have any hint, what to do ?

EDIT: listing from terminal:

pkg-config --libs opencv
/opt/local/lib/libopencv_calib3d.dylib /opt/local/lib/libopencv_contrib.dylib /opt/local/lib/libopencv_core.dylib /opt/local/lib/libopencv_features2d.dylib /opt/local/lib/libopencv_flann.dylib /opt/local/lib/libopencv_gpu.dylib /opt/local/lib/libopencv_highgui.dylib /opt/local/lib/libopencv_imgproc.dylib /opt/local/lib/libopencv_legacy.dylib /opt/local/lib/libopencv_ml.dylib /opt/local/lib/libopencv_nonfree.dylib /opt/local/lib/libopencv_objdetect.dylib /opt/local/lib/libopencv_photo.dylib /opt/local/lib/libopencv_stitching.dylib /opt/local/lib/libopencv_superres.dylib /opt/local/lib/libopencv_ts.a /opt/local/lib/libopencv_video.dylib /opt/local/lib/libopencv_videostab.dylib

pkg-config --libs-only-L opencv

should by this a problem ? why -L is empty ?

Was it helpful?

Solution 2

mistake was between chair and keyboard...

i proceed acording to this advice Qt5.1/Qt5.2 + Mac OS 10.9 (Mavericks) + XCode 5.0.2, Undefined symbols for architecture x86_64

but i only clicked to qmake and build, not to "clean all", so main.o was compiled for osx 10.6 all the time...

OTHER TIPS

You didn't post the complete error message and it's not clear which compiler you use. If you're using a version of GCC, I suppose the problem might not be actually finding the OpenCV libraries (because it seems you're doing that correctly), but missing symbols in these libraries.

This might be due to the fact that Apple changed the default C++ runtime library to libc++ in 10.9. This change also introduces a different C++ symbol mangling scheme, i.e. the symbols in the OpenCV library built with clang against libc++ are different from what g++ would produce given the OpenCV headers – I believe that is the reason why you're seeing missing symbols.

Even if the symbol mangling was the same, compiling with g++ against OpenCV from MacPorts is not a good idea, because g++ only supports libstdc++ as runtime, which would mean your own software would be trying to use libstdc++ and OpenCV would try to use libc++ as C++ runtime. This would mix two different (and incompatible) runtimes in the same process and lead to undefined behavior (random crashes at best).

In summary, compiling C++ code with g++ in 10.9 and above means you will also have to manually compile all the libraries you need with g++. As an alternative, try using clang++ as compiler instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top