在 Xcode 中链接 OpenCV 时,架构 x86_64 的未定义符号错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16078512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:47:27  来源:igfitidea点击:

Undefined symbols for architecture x86_64 error when linking OpenCV in Xcode

xcodeopencv

提问by sietschie

I have a problem linking OpenCV in Xcode. I installed OpenCV using brew:

我在 Xcode 中链接 OpenCV 时遇到问题。我使用 brew 安装了 OpenCV:

brew tap homebrew/science
sudo brew install opencv

I started a new Xcode commandline project, added /usr/local/liband /usr/local/includeto library and header search path. I also added the output of pkg-config --libs opencvto other linker options.

我开始了一个新的 Xcode 命令行项目,将/usr/local/lib和添加/usr/local/include到库和头文件搜索路径。我还添加了pkg-config --libs opencvto的输出other linker options

But when I try to compile this small sample program:

但是当我尝试编译这个小示例程序时:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char *argv[])
{
    cv::Mat test;
    cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    cv::waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
} 

i get the following linker error:

我收到以下链接器错误:

Undefined symbols for architecture x86_64:
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64

But I am able to compile the program on the command line using

但我能够在命令行上使用编译程序

g++ `pkg-config --cflags --libs opencv` prog.cpp

So I think the problem is the way Xcode is set up. But I am not able to determine what exactly is different between the way Xcode compiles and my commandline argument.

所以我认为问题在于 Xcode 的设置方式。但是我无法确定 Xcode 编译方式和我的命令行参数之间到底有什么不同。

Does anyone know the reason for this error or has an idea what I could try to investigate the problem?

有谁知道这个错误的原因或知道我可以尝试调查这个问题吗?

回答by Daniel Martín

In Xcode create a new group inside your project, right click it, choose Add Files to Project, navigate to the /usr/local/libfolder and add the following basic libraries:

在 Xcode 中在你的项目中创建一个新组,右键单击它,选择Add Files to Project,导航到该/usr/local/lib文件夹并添加以下基本库:

libopencv_core.dylib, libopencv_ml.dylib, libopencv_video.dylib

libopencv_core.dylib、libopencv_ml.dylib、libopencv_video.dylib

In previous versions of OpenCV the library names could be:

在以前版本的 OpenCV 中,库名称可能是:

libcxcore.dylib, libcvaux.dylib, libcv.dylib, libhighgui.dylib, libml.dylib

libcxcore.dylib、libcvaux.dylib、libcv.dylib、libhighgui.dylib、libml.dylib

(The libraries may be in another path depending on the method you used to install OpenCV on your Mac.)

(这些库可能位于另一个路径中,具体取决于您在 Mac 上安装 OpenCV 所使用的方法。)

Edit:

编辑:

The above lines should not be necessary if you are including the dynamic libraries in the linking phase as the OP explains.

如果您按照 OP 的说明在链接阶段包含动态库,则不需要上述行。

Select your project, go to the Build Settingstab, filter by c++ standard library, and set this parameter to libstdc++ (GNU C++ standard library).

选择您的项目,转到Build Settings选项卡,按c++ standard library过滤,并将此参数设置为libstdc++ (GNU C++ standard library)

回答by Galina Baytman

I've set the Build Settings -> c++ standard library to Compiler Default. The errors disappeared.

我已将 Build Settings -> c++ 标准库设置为Compiler Default。错误消失了。

回答by cloudsurfin

I had to add the following libraries (build phases -> link binary with libraries) so OpenCV would build:

我必须添加以下库(构建阶段 -> 将二进制文件与库链接),以便 OpenCV 构建:

  • Accelerate
  • AssetsLibrary
  • AVFoundation
  • CoreGraphics
  • CoreImage
  • CoreMedia
  • CoreVideo
  • QuartzCore
  • UIKit
  • Foundation
  • 加速
  • 资产库
  • AV基金会
  • 核心图形
  • 核心图像
  • 核心媒体
  • 核心视频
  • 石英核心
  • 用户界面工具包
  • 基础

Ref: http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing

参考:http: //docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing

回答by Chuyang

For me, I use opencv in xcode for iOS, and I solve the errors by using the opencv through pod install rather than offline opencv framework.

对我来说,我在iOS的xcode中使用opencv,我通过pod install而不是离线opencv框架使用opencv来解决错误。

You can have a try by adding the opencv pod text below:

您可以通过添加下面的 opencv pod 文本来尝试:

pod 'OpenCV', '2.4.9'