Xcode中有关OpenCV的链接错误

Posted

技术标签:

【中文标题】Xcode中有关OpenCV的链接错误【英文标题】:Link Error about OpenCV in Xcode 【发布时间】:2011-10-09 04:03:35 【问题描述】:

我正在使用 XCode4 和 OpenCV 2.3。 我已经编译了 OpenCV 的副本。这是 .a 的信息:

libopencv_core.a: Mach-O universal binary with 3 architectures
libopencv_core.a (for architecture armv7):  current ar archive random library
libopencv_core.a (for architecture armv6):  current ar archive random library
libopencv_core.a (for architecture i386):   current ar archive random library

我将它拖到Frameworks 目录中,并设置正确的库搜索路径。 但是我编译的时候就出现了这样的错误:

OpenCV 库中使用的 STL 似乎没有链接到 .a 文件中。 如何解决?

谢谢,鞠躬。

架构 armv7 的未定义符号:

"___cxa_begin_catch", referenced from:
      icvReadSeq(CvFileStorage*, CvFileNode*)       in libopencv_core.a(persistence.o)
  "typeinfo for std::exception", referenced from:
      typeinfo for cv::Exceptionin libopencv_core.a(alloc.o)
  "operator delete(void*)", referenced from:
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
  "_gzputs", referenced from:
      icvPuts(CvFileStorage*, char const*)in libopencv_core.a(persistence.o)
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_M_destroy(std::allocator<char> const&)", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
      _cvCreateMatHeader in libopencv_core.a(array.o)
      _cvGetImageCOI in libopencv_core.a(array.o)
      _cvSetImageCOI in libopencv_core.a(array.o)
      _cvGetSize in libopencv_core.a(array.o)
      ...
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      cv::Exception::Exception(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)in libopencv_core.a(array.o)
      _cvRegisterModule in libopencv_core.a(system.o)
  "___cxa_throw", referenced from:
      cv::error(cv::Exception const&)in libopencv_core.a(system.o)
  "___cxa_end_catch", referenced from:
      icvReadSeq(CvFileStorage*, CvFileNode*)       in libopencv_core.a(persistence.o)
  "_cvCvtColor", referenced from:
      -[cviosViewController CreateIplImageFromUIImage:] in cviosViewController.o
  "vtable for std::exception", referenced from:
      cv::Exception::Exception(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)in libopencv_core.a(array.o)
      cv::error(cv::Exception const&)in libopencv_core.a(system.o)
  "operator new[](unsigned long)", referenced from:
      cv::mixChannels(cv::Mat const*, unsigned long, cv::Mat*, unsigned long, int const*, unsigned long)in libopencv_core.a(convert.o)
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      cv::Exception::Exception(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)in libopencv_core.a(array.o)
      cv::error(cv::Exception const&)in libopencv_core.a(system.o)
      _cvRegisterModule in libopencv_core.a(system.o)
  "vtable for __cxxabiv1::__si_class_type_info", referenced from:
      typeinfo for cv::Exceptionin libopencv_core.a(alloc.o)
  "std::terminate()", referenced from:
      cv::error(cv::Exception const&)in libopencv_core.a(system.o)
  "___cxa_allocate_exception", referenced from:
      cv::error(cv::Exception const&)in libopencv_core.a(system.o)
  "___gxx_personality_sj0", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      _cvCreateMatHeader in libopencv_core.a(array.o)
      _cvGetImageCOI in libopencv_core.a(array.o)
      _cvSetImageCOI in libopencv_core.a(array.o)
      _cvGetSize in libopencv_core.a(array.o)
      _cvGetDims in libopencv_core.a(array.o)
      _cvGetElemType in libopencv_core.a(array.o)
      ...
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      _cvCreateMatHeader in libopencv_core.a(array.o)
      _cvGetImageCOI in libopencv_core.a(array.o)
      _cvSetImageCOI in libopencv_core.a(array.o)
      _cvGetSize in libopencv_core.a(array.o)
      _cvGetDims in libopencv_core.a(array.o)
      _cvGetElemType in libopencv_core.a(array.o)
      ...
  "operator delete[](void*)", referenced from:
      cv::mixChannels(cv::Mat const*, unsigned long, cv::Mat*, unsigned long, int const*, unsigned long)in libopencv_core.a(convert.o)
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_empty_rep_storage", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
      _cvCreateMatHeader in libopencv_core.a(array.o)
      _cvGetImageCOI in libopencv_core.a(array.o)
      _cvSetImageCOI in libopencv_core.a(array.o)
      _cvGetSize in libopencv_core.a(array.o)
      ...
  "std::exception::~exception()", referenced from:
      cv::OutOfMemoryError(unsigned long)in libopencv_core.a(alloc.o)
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
      cv::Exception::~Exception()in libopencv_core.a(alloc.o)
      cv::Exception::Exception(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)in libopencv_core.a(array.o)
      cv::error(cv::Exception const&)in libopencv_core.a(system.o)
      _cvRegisterModule in libopencv_core.a(system.o)

ld:未找到架构 armv7 的符号 collect2: ld 返回 1 个退出状态

顺便说一句:当我将文件名设置为 .mm 时,链接器会报告内部编译器错误。总线错误。

【问题讨论】:

永远不会链接 .a 文件。您是否尝试将 c++ 库链接到您的可执行文件。使用 gcc 这将是添加 -lstdc++。 大卫,你让我头疼不已。也许这应该是官方回答而不是评论。会有+1作为答案。改为为评论 +1。 【参考方案1】:

您可以通过“Build Phases -> Link Binary With Libraries”设置与 c++ 标准库的链接

【讨论】:

【参考方案2】:

我知道答案了。

OpenCV 中的 MIN 宏与 Xcode 中预定义的 MIN 冲突。

将 .m 文件重命名为 .mm,并将所有 MIN 函数更改为新名称,例如 OpenCV 头文件中的 CV_MIN。

检查。

【讨论】:

【参考方案3】:

Bing,您对 OpenCV 的 MIN(和 MAX)宏是正确的。从 v 2.0 开始,我可能不得不重命名它们 :)

但是,没有必要将所有 .m 文件重命名为 .mm 文件,因为 Xcode 默认对它们的处理方式不同 - 作为 Objective-C++ 文件 - 这并不总是您想要的。

最好链接 std c++ 库,正如 David 上面指出的那样。

【讨论】:

【参考方案4】:

至少在 xcode 4.5 中值得一提的是,您必须在目标设置中将 C++ 标准库更改为“libstdc++”才能编译 openCV。

【讨论】:

【参考方案5】:

放弃对 iOS 4.3 的支持并使用“libc++”可能也会有所帮助。

【讨论】:

以上是关于Xcode中有关OpenCV的链接错误的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV3.2 编译错误 MAC OS X

Xcode构建错误链接器命令失败,退出代码为1(使用-v查看调用)

归档有关符号链接的 iOS 项目时出错

Qt-Creator 中 OpenCV 程序中的链接器错误

opencv2 IOS框架链接器错误

使用某些 OpenCV 函数“阈值”Eclipse 时出现链接错误