0

When I try to build an OpenCV application:

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

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    if (argc < 2) {
        cout << "usage: " << argv[0] << " image.png" << endl;
        return 1;
    }
    Mat im = imread(argv[1]);
    return 0;
}

I get a linker error:

bash-3.2$ g++ `pkg-config --cflags --libs opencv` fs.cpp
Undefined symbols for architecture x86_64:
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in fs-EK7vjB.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here are the libraries I'm linking against (OpenCV installed via homebrew):

bash-3.2$ pkg-config --libs opencv
/usr/local/Cellar/opencv/2.4.5/lib/libopencv_calib3d.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_contrib.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_core.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_features2d.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_flann.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_gpu.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_highgui.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_imgproc.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_legacy.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_ml.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_nonfree.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_objdetect.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_ocl.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_photo.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_stitching.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_superres.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_ts.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_video.dylib /usr/local/Cellar/opencv/2.4.5/lib/libopencv_videostab.dylib

Slightly more readable list:

bash-3.2$ for f in $(pkg-config --libs opencv); do echo $(basename $f); done
libopencv_calib3d.dylib
libopencv_contrib.dylib
libopencv_core.dylib
libopencv_features2d.dylib
libopencv_flann.dylib
libopencv_gpu.dylib
libopencv_highgui.dylib
libopencv_imgproc.dylib
libopencv_legacy.dylib
libopencv_ml.dylib
libopencv_nonfree.dylib
libopencv_objdetect.dylib
libopencv_ocl.dylib
libopencv_photo.dylib
libopencv_stitching.dylib
libopencv_superres.dylib
libopencv_ts.dylib
libopencv_video.dylib
libopencv_videostab.dylib

What am I doing wrong, and how can I make things work?

2 Answers 2

1

It looks like you are not linking the right libs for your system. Try CMake to create your project files (please refer to my answer here for details).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply. These are the only libs I have. If they're not the right libs, then what good will CMake do? How can I check if these are indeed the right libs or not?
Building the sources is a nice way to make sure. Kudos.
0

It looks like the homebrew install of OpenCV is broken. I built the library from source and it worked.

  1. Download and unpack the most recent tarball from http://opencv.org/
  2. mkdir opencv-2.4.7/build
  3. cd opencv-2.4.7/build
  4. ccmake - set all the relevant options, in particular CMAKE_INSTALL_PREFIX
  5. make
  6. make install
  7. export PKG_CONFIG_PATH=/Users/misha/lib/pkgconfig (or wherever you've installed the library)
  8. pkg-config --libs --cflags opencv - confirm that pkgconfig picks up your newly installed libraries
  9. build the application

Also, you may want to remove the broken homebrew opencv:

brew uninstall opencv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.