Apple Mach-O linker and multiple “undefined symbols” error using OpenCV

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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 >::flush()", referenced from:       cv::gpu::error(char const*, char const*, int, char const*) in opencv2(gpumat.o)   "std::__1::basic_ostream >::sentry::sentry(std::__1::basic_ostream >&)", referenced from:       std::__1::basic_ostream >& std::__1::operator >(std::__1::basic_ostream >&, char const*) in opencv2(gpumat.o)   "std::__1::basic_ostream >::sentry::~sentry()", referenced from:       std::__1::basic_ostream >& std::__1::operator >(std::__1::basic_ostream >&, char const*) in opencv2(gpumat.o)   "std::__1::basic_ostream >::operator >& std::__1::operator >(std::__1::basic_ostream >&, char const*) in opencv2(gpumat.o)   "std::__1::ios_base::clear(unsigned int)", referenced from:       std::__1::basic_ostream >& std::__1::operator >(std::__1::basic_ostream >&, char const*) in opencv2(gpumat.o)   "std::__1::__vector_base_common::__throw_length_error() const", referenced from:       __ZNSt3__16vectorImNS_9allocatorImEEE6assignIPmEENS_9enable_ifIXsr21__is_forward_iteratorIT_EE5valueEvE4typeES7_S7_ in opencv2(matrix.o)       std::__1::vector >::vector(unsigned long) in opencv2(matrix.o)       std::__1::vector >::__append(unsigned long) in opencv2(matrix.o)       std::__1::vector<:vec>, std::__1::allocator<:vec> > >::vector(unsigned long) in opencv2(matrix.o)       std::__1::vector >::vector(unsigned long) in opencv2(matrix.o)       std::__1::vector<:vec>, std::__1::allocator<:vec> > >::__append(unsigned long) in opencv2(matrix.o)       std::__1::vector<:vec>, std::__1::allocator<:vec> > >::__append(unsigned long) in opencv2(matrix.o)       ...   "std::__1::basic_string, std::__1::allocator >::__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_ 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, std::__1::allocator >::~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_ 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, std::__1::allocator >::resize(unsigned long, char)", referenced from:       __ZL8icvCloseP13CvFileStoragePNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE in opencv2(persistence.o)   "std::__1::basic_string, std::__1::allocator >::__init(wchar_t const*, unsigned long)", referenced from:       cv::toUtf16(std::__1::basic_string, std::__1::allocator > const&) in opencv2(persistence.o)   "std::__1::basic_string, std::__1::allocator >::__init(char const*, unsigned long, unsigned long)", referenced from:       std::__1::basic_string, std::__1::allocator > std::__1::operator+, std::__1::allocator >(std::__1::basic_string, std::__1::allocator > const&, char const*) in opencv2(system.o)   "std::__1::basic_string, std::__1::allocator >::append(char const*)", referenced from:       cv::tempfile(char const*) in opencv2(system.o)   "std::__1::basic_string, std::__1::allocator >::append(char const*, unsigned long)", referenced from:       std::__1::basic_string, std::__1::allocator > std::__1::operator+, std::__1::allocator >(std::__1::basic_string, std::__1::allocator > const&, char const*) in opencv2(system.o)   "std::__1::basic_string, std::__1::allocator >::assign(char const*)", referenced from:       cv::tempfile(char const*) in opencv2(system.o)   "std::__1::basic_string, std::__1::allocator >::basic_string(std::__1::basic_string, std::__1::allocator > const&)", referenced from:       cv::Exception::Exception(int, std::__1::basic_string, std::__1::allocator > const&, std::__1::basic_string, std::__1::allocator > const&, std::__1::basic_string, std::__1::allocator > 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, std::__1::allocator > const&) in opencv2(persistence.o)   "std::__1::basic_string, std::__1::allocator >::operator=(std::__1::basic_string, std::__1::allocator > const&)", referenced from:       cv::Exception::formatMessage() in opencv2(system.o)       cv::tempfile(char const*) in opencv2(system.o)       cv::operator, std::__1::allocator > 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   #ifndef __IPHONE_5_0 #warning "This project uses features only available in iOS SDK 5.0 and later." #endif  #ifdef __cplusplus #import  #endif  #ifdef __OBJC__     #import      #import  #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"



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!