简体   繁体   中英

OpenCV C++ error in Xcode

I have built the OpenCV libraries using the cmake build system as described here and have added the header, '.a' and '.dylib' files to my terminal c++ project. However when I run the code below (got it from http://iphone-cocoa-objectivec.blogspot.com/2009/01/using-opencv-for-mac-os-in-xcode.html ), it gives me the errors below. Has anyone got any advice? Any help will be much appreciated.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

int main()
{

    //get the image from the directed path
    IplImage* img = cvLoadImage("/Users/somedir/Programming/TestProj/fruits.jpg", 1);

    //create a window to display the image
    cvNamedWindow("picture", 1);

    //show the image in the window
    cvShowImage("picture", img);

    //wait for the user to hit a key
    cvWaitKey(0);

    //delete the image and window
    cvReleaseImage(&img);
    cvDestroyWindow("picture");

    //return
    return 0;
}

Errors

Undefined symbols:
  "_cvLoadImage", referenced from:
      _main in main.o
  "_cvNamedWindow", referenced from:
      _main in main.o
  "_cvReleaseImage", referenced from:
      _main in main.o
  "_cvShowImage", referenced from:
      _main in main.o
  "_cvDestroyWindow", referenced from:
      _main in main.o
  "_cvWaitKey", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

First of all, don't build the libs with CMake, better to get them from macports on mac, you can easily update to the newer version with a one-liner...

Plus, if you would use the cv::Mat interfaces with
#include <opencv2/core/core.hpp> and #include <opencv2/highgui/highgui.hpp> things would go better... ;) Include the dylib libraries with versions at the end of their name. (I think the version-less dylibs are for the old interface #include)

For starters:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{

    //get the image from the directed path
    Mat img = loadImage("/Users/somedir/Programming/TestProj/fruits.jpg");

    //show the image in the window
    imshow("picture", img);

    //wait for the user to hit a key
    waitKey(0);
    //delete the image and window (automatic)
    return 0;
}

Avoid using Xcode with OpenCV 2.0. If using OpenCV use Windows and also use OpenCV 1.1. It will save a lot of headache. When 2.0/Mac are better documented then transition onto Mac platform/2.0 version. The book (O'Reilly) is good - covers v1.1. The next installment for 2.0 should follow soon. 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM