Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined symbols for architecture x86_64 error when linking OpenCV in Xcode

Tags:

xcode

opencv

I have a problem linking OpenCV in Xcode. I installed OpenCV using brew:

brew tap homebrew/science
sudo brew install opencv

I started a new Xcode commandline project, added /usr/local/lib and /usr/local/include to library and header search path. I also added the output of pkg-config --libs opencv to other linker options.

But when I try to compile this small sample program:

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

int main(int argc, char *argv[])
{
    cv::Mat test;
    cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    cv::waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
} 

i get the following linker error:

Undefined symbols for architecture x86_64:
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64

But I am able to compile the program on the command line using

g++ `pkg-config --cflags --libs opencv` prog.cpp

So I think the problem is the way Xcode is set up. But I am not able to determine what exactly is different between the way Xcode compiles and my commandline argument.

Does anyone know the reason for this error or has an idea what I could try to investigate the problem?

like image 445
sietschie Avatar asked Apr 18 '13 08:04

sietschie


3 Answers

In Xcode create a new group inside your project, right click it, choose Add Files to Project, navigate to the /usr/local/lib folder and add the following basic libraries:

libopencv_core.dylib, libopencv_ml.dylib, libopencv_video.dylib

In previous versions of OpenCV the library names could be:

libcxcore.dylib, libcvaux.dylib, libcv.dylib, libhighgui.dylib, libml.dylib

(The libraries may be in another path depending on the method you used to install OpenCV on your Mac.)

Edit:

The above lines should not be necessary if you are including the dynamic libraries in the linking phase as the OP explains.

Select your project, go to the Build Settings tab, filter by c++ standard library, and set this parameter to libstdc++ (GNU C++ standard library).

like image 117
Daniel Martín Avatar answered Nov 04 '22 04:11

Daniel Martín


I've set the Build Settings -> c++ standard library to Compiler Default. The errors disappeared.

like image 7
Galina Baytman Avatar answered Nov 04 '22 03:11

Galina Baytman


I had to add the following libraries (build phases -> link binary with libraries) so OpenCV would build:

  • Accelerate
  • AssetsLibrary
  • AVFoundation
  • CoreGraphics
  • CoreImage
  • CoreMedia
  • CoreVideo
  • QuartzCore
  • UIKit
  • Foundation

Ref: http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing

like image 1
cloudsurfin Avatar answered Nov 04 '22 03:11

cloudsurfin