Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up OpenCV in QT on OSX

Tags:

macos

opencv

qt

I am trying to set up OpenCV to work with QT on OSX 10.7.5/MacbookPro 2.5 Ghz Intel Core 2 Duo. I've seen a few related question here (How to link opencv in QtCreator and use Qt library and How do you set up OpenCV with QT in OSX?) but there isn't much detail. On the QT site and in my web searches all of the information seems to be about Windows or Linux.


I've listed what my setup and code is below. When I run the code I am getting an error: :-1: error: symbol(s) not found for architecture x86_64

Does this mean it's not finding stuff because the paths are wrong or because I may have built openCV for x86_32? Is there a way of checking the latter? I'm not so savvy with parts of the configuration and setup process.


Update 2

The console output is below - perhaps the error is obvious?

02:44:38: Running steps for project RP_openCV_01...
02:44:38: Configuration unchanged, skipping qmake step.
02:44:38: Starting: "/usr/bin/make" 
clang++ -headerpad_max_install_names -mmacosx-version-min=10.6 -o RP_openCV_01 main.o   -L/usr/local/lib -1ibopencv_core.2.4.6,dylib -1ibopencv_imgproc.2.4.6.dylib -F/Users/rise/Qt5.0.2/5.0.2/clang_64/lib -framework QtCore 
clang: warning: argument unused during compilation: '-1ibopencv_core.2.4.6,dylib'
clang: warning: argument unused during compilation: '-1ibopencv_imgproc.2.4.6.dylib'
Undefined symbols for architecture x86_64:
  "cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
      _main in main.o
  "cv::namedWindow(std::string const&, int)", referenced from:
      _main in main.o
  "cv::Mat::deallocate()", referenced from:
      _main in main.o
  "cv::imread(std::string const&, int)", referenced from:
      _main in main.o
  "cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
      _main in main.o
  "cv::waitKey(int)", referenced from:
      _main in main.o
  "cv::fastFree(void*)", 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: *** [RP_openCV_01] Error 1
02:44:38: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project RP_openCV_01 (kit: Desktop Qt 5.0.2 clang 64bit)
When executing step 'Make'

What I have so far (the tl;dr):

  • built and installed the latest version of openCV (2.4.6) from source and tested it with some command line programs.

  • installed the latest version of QT (5.1) and am able to run all the examples, etc.

  • specified the paths in the project file (below)

QT       += core
QT       -= gui

TARGET = RP_openCV_01
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
SOURCES += main.cpp

INCLUDEPATH += /usr/local/include\
LIBS += -L/usr/local/lib
  • tried to specify the path in the project settings (below). I added /usr/local/include and /usr/local/lib

enter image description here

The simple example code in main.cpp

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main() {

    // read an image
    cv::Mat image= cv::imread("dog.jpg");
    // create image window named "My Image"
    cv::namedWindow("My Image");
    // show the image on window
    cv::imshow("My Image", image);
    // wait key for 5000 ms
    cv::waitKey(5000);

    return 1;
}

Update 1

Another thing I tried based on a tutorial was to specify the libs in the QT profile (as in image below). The tutorial was for Windows though and I didn't know if or how OSX is different. Where in the Windows example it is -1ibopencv_core246d I tried it with and without the separating periods but without the "d". Of course the full name of a lib is "libopencv_core.2.4.6.dylib" etc.

These sorts of important details always trip me up but it's often assumed in tutorials that one knows this stuff.

 LIBS += -L/usr/local/lib \
     -1ibopencv_core.2.4.6 \
     -1ibopencv_imgproc.2.4.6 \
     -1ibopencv_features2d.2.4.6 \
     -1ibopencv_highgui.2.4.6

enter image description here

like image 951
spring Avatar asked Jul 08 '13 02:07

spring


2 Answers

Just a naive mistake

The proper declaration is:

LIBS += -L/usr/local/lib \
     -lopencv_core \
     -lopencv_imgproc \
     -lopencv_features2d\
     -lopencv_highgui
like image 153
spring Avatar answered Sep 21 '22 06:09

spring


I just stumbled over the same problem. Here is how I solved it:

As sansuiso already suggested, the problem appears to be the C++-STD-lib you are trying to link against. You are probably trying to link libc++ (LLVM ...) but instead libstdc++ (GNU C++ ...) should be used.

Using the Qt Creator you can add the following to your .pro-file:

mac: 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
}

This answer could also be interesting for your problem: https://stackoverflow.com/a/12852913/1141118

Hope this helps!

like image 31
nils Avatar answered Sep 20 '22 06:09

nils