13

I have installed opencv with macports following the directions here: Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode

I have also search and tried every other variation of this on stackexchange and google, but this seems to get me closest.

It seems to work for some things, but not for sample code that ships with 2.4.2. Note that I have added ALL opencv 2.4.2 dylibs Link Binary with Libraries.

For example, the following will compile and run:

#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;
}

However, when I try to build any of the samples, such as the display_image.cpp, example, as follows, I get link errors.

-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;

}

I get the following errors:

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)

Any idea how to resolve this?

Community
  • 1
  • 1
glerg
  • 139
  • 1
  • 1
  • 4
  • Anthony and Marco gave you the correct answer below. Its one of those things that, once you experience it, you know it forever. You should accept an answer to help future visitors. – jww Aug 13 '14 at 05:02

3 Answers3

42

I had the same problem. A build setting default seems to be different in Xcode 4.5.

Under "Build Settings"--> Apple LLVM compiler 4.1 - Language >

C++ Standard Library:= Change from libc++ (LLVM ...) to libstdc++ (GNU C++ ...).

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Anthony Maida
  • 591
  • 3
  • 8
  • Could you develop more on the answer I didn't understand why do we have to change from LLVM to GNU C++. @glerg accept the answer. – BRabbit27 Apr 02 '13 at 22:06
  • 1
    It worked for me in quite opposite way: I changed standard library from GNU to LLVM. – Valeriy Van Apr 04 '13 at 17:24
  • 1
    @BRabbit27 "nm" command revealed that libc++ has a different symbol naming convention compared to libstdc++. So, we must choose the same C++ library that OpenCV was compiled with, otherwise the linker will fail to resolve symbols. Here is a screenshot showing different naming between libc++ & libstdc++ → [link](http://i.stack.imgur.com/Mnexn.png) – Shigerello May 17 '13 at 04:53
  • I'm having exactly this problem. My name mangling is mismatched just like the BRappit27's screenshot shows. But I don't understand how the the c++ library that's being used has anything to do with how my program's binary gets linked against the opencv libraries. Isn't it compiler flags that are going to determine name mangling. Can someone please explain? – Steve Jun 13 '13 at 13:25
  • I wish I could accept this answer. Saved me several hours of frustration. – cfischer Aug 27 '13 at 13:27
  • I would also like a more deeper explanation. I know nothing about name mangling and stuff. What is the "nm" command. Isn't it supposed that the same compiler I used to create the libraries is the same as Xcode uses as default? – BRabbit27 Oct 07 '13 at 08:38
  • I know this is old, but it's still an issue so I'll elaborate - the issue is that C++ name mangling requires embedding the type of function parameters in the mangled names. And GNU and LLVM C++ std lib have a different type for std::string (probably among others). So any function that has a std::string in its parameters will end up with a different mangled name. cv::imshow is one such function, hence the linker is looking for a function with one mangled name, but the version in higui has a different name. – JoshG79 Jan 02 '14 at 19:52
4

It's very likely that OpenCV has not been compiled with C++11 settings, while the program is. Set the build of your tool without C++11 switches (i.e. -std=c++11 -stdlib=libc++).

Marco
  • 2,389
  • 2
  • 16
  • 19
0

Try to manually add the directory where port puts all the dylibs (/opt/local/lib if I'm not getting wrong) in Build Settings->Library search path. This should fix the linking problem.

Manlio
  • 10,768
  • 9
  • 50
  • 79
  • Thanks, but I had already done that. Without it, I get a different error: ld: library not found for -lopencv_calib3d.2.4.2 clang: error: linker command failed with exit code 1 (use -v to see invocation) – glerg Oct 07 '12 at 18:09