在xcode 4.5.1上链接库OpenCV 2.4.2

时间:2012-10-07 15:27:11

标签: xcode opencv

我已按照此处的说明安装了带有macport的opencv:Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode

我还在stackexchange和google上搜索并尝试了其他所有变体,但这似乎让我最接近。

它似乎适用于某些事情,但不适用于2.4.2附带的示例代码。请注意,我已将所有opencv 2.4.2 dylibs链接二进制文件添加到库中。

例如,以下内容将编译并运行:

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

int main ( int argc, char **argv )
{
    cvNamedWindow( "My Window", 1 );
    IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
    CvFont font;
    double hScale = 1.0;
    double vScale = 1.0;
    int lineWidth = 1;
    cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
           hScale, vScale, 0, lineWidth );
    cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
          cvScalar( 255, 255, 0 ) );
    cvShowImage( "My Window", img );
    cvWaitKey();
    return 0;
}

但是,当我尝试构建任何示例时,例如display_image.cpp,例如,如下所示,我得到了链接错误。

-DOES NOT WORK -

 #include <stdio.h>
 #include <iostream>
 #include "opencv2/imgproc/imgproc.hpp"
 #include "opencv2/highgui/highgui.hpp"
 #include "opencv2/flann/miniflann.hpp"

 using namespace cv; // all the new API is put into "cv" namespace. Export its content
 using namespace std;
 using namespace cv::flann;

static void help()
{
    cout <<
    "\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
    "It shows reading of images, converting to planes and merging back, color conversion\n"
    "and also iterating through pixels.\n"
    "Call:\n"
    "./image [image-name Default: lena.jpg]\n" << endl;
}

int main(int argc, char *argv[])
{
    help();
    const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
    Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
    if(img.empty())
    {
        fprintf(stderr, "Can not load image %s\n", imagename);
        return -1;
    }
    if( !img.data ) // check if the image has been loaded properly
        return -1;

    Mat img_yuv;
    cvtColor(img, img_yuv, CV_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically

    vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
    split(img_yuv, planes); // split the image into separate color planes

    imshow("image with grain", img);

    waitKey();

    return 0;

}

我收到以下错误:

Undefined symbols for architecture x86_64:
 "cv::split(cv::Mat const&, std::__1::vector<cv::Mat, std::__1::allocator<cv::Mat> >&)", referenced from:
  _main in main1.o
 "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
  _main in main1.o
 "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
  _main in main1.o
 ld: symbol(s) not found for architecture x86_64
 clang: error: linker command failed with exit code 1 (use -v to see invocation)

知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:40)

我遇到了同样的问题。 Xcode 4.5中的构建设置默认值似乎不同。

在“构建设置”下 - &gt; Apple LLVM编译器4.1 - 语言&gt;

C ++标准库:= libc ++ (LLVM ...)更改为 libstdc ++ (GNU C ++ ...)。

答案 1 :(得分:3)

OpenCV很可能没有用C ++ 11设置编译,而程序是。 在没有C ++ 11开关的情况下设置工具的构建(即-std = c ++ 11 -stdlib = libc ++)。

答案 2 :(得分:0)

尝试在Build Settings-&gt; Library搜索路径中手动添加端口放置所有dylib(/opt/local/lib,如果我没有出错)的目录。这应该可以解决链接问题。