Apple Mach-O linker and multiple "undefined symbols" error using OpenCV
Asked Answered
O

5

36

I am currently a real beginner in Objective C, and I am developing an app using the framework OpenCV.

I am following a tutorial, but while builing the project, I got several errors :

Undefined symbols for architecture i386:
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from:
      cv::gpu::error(char const*, char const*, int, char const*) in opencv2(gpumat.o)
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in opencv2(gpumat.o)
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in opencv2(gpumat.o)
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int)", referenced from:
      cv::gpu::error(char const*, char const*, int, char const*) in opencv2(gpumat.o)
  "std::__1::cerr", referenced from:
      cv::gpu::error(char const*, char const*, int, char const*) in opencv2(gpumat.o)
  "std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in opencv2(gpumat.o)
  "std::__1::ios_base::clear(unsigned int)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in opencv2(gpumat.o)
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      __ZNSt3__16vectorImNS_9allocatorImEEE6assignIPmEENS_9enable_ifIXsr21__is_forward_iteratorIT_EE5valueEvE4typeES7_S7_ in opencv2(matrix.o)
      std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >::vector(unsigned long) in opencv2(matrix.o)
      std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >::__append(unsigned long) in opencv2(matrix.o)
      std::__1::vector<cv::Vec<float, 2>, std::__1::allocator<cv::Vec<float, 2> > >::vector(unsigned long) in opencv2(matrix.o)
      std::__1::vector<int, std::__1::allocator<int> >::vector(unsigned long) in opencv2(matrix.o)
      std::__1::vector<cv::Vec<int, 128>, std::__1::allocator<cv::Vec<int, 128> > >::__append(unsigned long) in opencv2(matrix.o)
      std::__1::vector<cv::Vec<int, 64>, std::__1::allocator<cv::Vec<int, 64> > >::__append(unsigned long) in opencv2(matrix.o)
      ...
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
      cv::Mat::create(int, int const*, int) in opencv2(matrix.o)
      __ZN2cvL7setSizeERNS_3MatEiPKiPKmb in opencv2(matrix.o)
      cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&) in opencv2(matrix.o)
      cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&) in opencv2(matrix.o)
      cv::Mat::Mat(cv::Mat const&, cv::Range const*) in opencv2(matrix.o)
      cv::Mat::diag(int) const in opencv2(matrix.o)
      cv::Mat::Mat(_IplImage const*, bool) in opencv2(matrix.o)
      ...
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
      cv::Mat::create(int, int const*, int) in opencv2(matrix.o)
      __ZN2cvL7setSizeERNS_3MatEiPKiPKmb in opencv2(matrix.o)
      cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&) in opencv2(matrix.o)
      cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&) in opencv2(matrix.o)
      cv::Mat::Mat(cv::Mat const&, cv::Range const*) in opencv2(matrix.o)
      cv::Mat::diag(int) const in opencv2(matrix.o)
      cv::Mat::Mat(_IplImage const*, bool) in opencv2(matrix.o)
      ...
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::resize(unsigned long, char)", referenced from:
      __ZL8icvCloseP13CvFileStoragePNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE in opencv2(persistence.o)
  "std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >::__init(wchar_t const*, unsigned long)", referenced from:
      cv::toUtf16(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in opencv2(persistence.o)
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long, unsigned long)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > std::__1::operator+<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) in opencv2(system.o)
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::append(char const*)", referenced from:
      cv::tempfile(char const*) in opencv2(system.o)
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::append(char const*, unsigned long)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > std::__1::operator+<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) in opencv2(system.o)
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::assign(char const*)", referenced from:
      cv::tempfile(char const*) in opencv2(system.o)
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      cv::Exception::Exception(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) in opencv2(system.o)
      cv::tempfile(char const*) in opencv2(system.o)
      cv::Exception::Exception(cv::Exception const&) in opencv2(system.o)
      cv::operator<<(cv::FileStorage&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in opencv2(persistence.o)
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::operator=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      cv::Exception::formatMessage() in opencv2(system.o)
      cv::tempfile(char const*) in opencv2(system.o)
      cv::operator<<(cv::FileStorage&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in opencv2(persistence.o)
ld: symbol(s) not found for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am sure that the problem comes from the framework, but not the framework itself, but how it is link in the project, or...

I precise that I've linked it that way in the .pch file

 #import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

Don't really know what to do..

EDIT : forgot to tell that I renamed my .m file to .mm and my .h file to .hh Is there anything else we need to do to get an objective C++ projet ?

Oho answered 25/2, 2013 at 15:35 Comment(2)
Looks like you need to link in the C++ standard library.Law
But I've already changed the files' name to .mm and .hh, isn't supposed to be enough ?Oho
B
128

Building on Cthutu's answer, to add libc++.dylib to your project:

  1. Select your project (the blue file) in your project navigator (Command 1 if it's hidden)
  2. Select your target
  3. Go to Build Phases
  4. Expand "Link Binary With Libraries"
  5. Click the "+"
  6. Type libc++.dylib in the search bar.
  7. Select the libc++.dylib file and press "Add"
Buckskin answered 25/2, 2013 at 16:50 Comment(5)
Works for me, but adding libstdc++.6.0.9.dylib. Thanks!Niobe
1 hour of searching for this - many, many incorrect guesses by people asking the same question in different ways, for different libs. Finally ... this solution is the general solution for all C++ linking in Xcode5. Thank you so much!Ngo
Thanks Lance! Could u add that in Xcode 7 you need to use libc++.tbd (.dylib is not directly available)?Cense
this doesnt work for me , similar issue in CEF, any suggestions, mine is : #42992876Lee
I'm running into an error similar to this one when building my project with OpenCV. Could you perhaps take a look at my question about it?Strawworm
F
16

You're missing the standard C++ library. You need to link with it to get all the implementations for the STL classes you're getting errors for.

Add the libc++.dylib to your project.

Fart answered 25/2, 2013 at 16:36 Comment(4)
Thank you very much, that's what I needed to know ^^Oho
@Oho please consider marking an answer as accepted so that people in the future can find the answer that helped you out. Thanks.Buckskin
Ok, still not used to Stackover flow. DoneOho
or -I/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/lib/libc++.dylibTimm
T
5

This actually seems to hide the actual problem: the fact that your LLVM compiler may not choose a proper standard C++ library for compiling by default. You should go to

Project (or your Target) -> Build Settings -> Apple LLVM 6.0 - Language - C++ -> C++ Standard Library

and make sure that you pick libc++ as your choice, not just Compiler Default, since modern OpenCV library is linked against libc++. This will solve your problem without having to add the extra libc++.dylib. Including libc++.dylib framework as a workaround will not always work out if your Compiler Default actually does not choose libc++ library!

To check what "Compiler Default" is, you can do something like this. Mine somehow chooses c++98 as a default.

Toneytong answered 10/10, 2014 at 3:11 Comment(0)
G
4

The user can also add the libc++.tbd if libc++.dylib is not available in the "Link Binary With Libraries"

Thanks Mitesh

Groveman answered 9/6, 2016 at 7:31 Comment(0)
L
2

I had a similar problem, try removing "-lopencv_ts" from "Other Linker Flags"

Lankton answered 6/5, 2017 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.