Apple Mach-O linker and multiple “undefined symbol

2019-01-10 10:25发布

问题:

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 ?

回答1:

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"


回答2:

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.



回答3:

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.



回答4:

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

Thanks Mitesh



回答5:

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