xcode 如何在 Cocoa 应用程序中包含 OpenCV?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3810800/
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-14 19:47:33  来源:igfitidea点击:

How to include OpenCV in Cocoa Application?

cocoaxcodeopencv

提问by Fabian

when I create an xCode project with the 'Command Line Tool' c++ stdc++ template, i am able to include and compile opencv headers and run some code.

当我使用“命令行工具”c++ stdc++ 模板创建 xCode 项目时,我能够包含和编译 opencv 标头并运行一些代码。

But i want to use OpenCV in a 'Cocoa Application' context. When created with that template, i got compile errors when I include the OpenCV headers in main.mm. (I already changed main.m to main.mm, the '//NSApplicationMain(argc, (const char **) argv);' is commented out)

但我想在“可可应用程序”上下文中使用 OpenCV。使用该模板创建时,当我在 main.mm 中包含 OpenCV 头文件时出现编译错误。(我已经把 main.m 改成了 main.mm,'//NSApplicationMain(argc, (const char **) argv);' 被注释掉了)

One of those errors is: "Statement-expressions are allowed only inside functions"

其中一个错误是:“语句表达式只允许在函数内部使用”

I suppose its some kind of compiler version error, but when i compare the project build settings i cant find differences.

我想它是某种编译器版本错误,但是当我比较项目构建设置时,我找不到差异。

Do you have any ideas/expertise?

你有什么想法/专业知识吗?

回答by Ian Charnas

I ran into the same problem, I spent 2 days doing serious system tracing and as expected, the solution was so simple that an 8-year-old could have found it more quickly than I did.

我遇到了同样的问题,我花了 2 天时间进行严重的系统跟踪,正如预期的那样,解决方案非常简单,一个 8 岁的孩子可以比我更快地找到它。

Ready for this?

准备好了吗?

In the .mm file where you want to use OpenCV, you need to do your #include "opencv2/opencv.hpp"BEFORE any other includes. That's it! Just move it up a line and watch your problem magically disappear faster than that rug that really tied the room together.

在要使用 OpenCV 的 .mm 文件中,您需要#include "opencv2/opencv.hpp"在任何其他包含之前执行。就是这样!只需将它向上移动一条线,然后看着您的问题神奇地消失,而不是真正将房间连接在一起的地毯。

This is for OpenCV 2.2 by the way, hence the new include file. Also if your XCode project uses a prefix header file (look in "Other Sources" for a "YourProjectName_Prefix.pch" file), then you'll need to put your #include "opencv2/opencv.hpp"there instead of in any other file.

顺便说一下,这是针对 OpenCV 2.2 的,因此是新的包含文件。此外,如果您的 XCode 项目使用前缀头文件(在“其他来源”中查找“YourProjectName_Prefix.pch”文件),那么您需要将您的#include "opencv2/opencv.hpp"文件放在那里而不是任何其他文件中。

回答by user671435

Ian Charnas's Answer is correct, but there is one modification I would make.

Ian Charnas 的回答是正确的,但我会做一个修改。

This article has a more specific solution, and an explanation of why you need to do this. http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

这篇文章有一个更具体的解决方案,并解释了为什么需要这样做。 http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

// Add this new section BEFORE the #import statements for UIKit and Foundation

// 在 UIKit 和 Foundation 的 #import 语句之前添加这个新部分

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

回答by propagandaapparat

Even if you rename your "main.m" to "main.mm" and moving the "#include opencv2/opencv.hpp" to the top (in the main file), the preprocessor will insert cocoa.h first because of the precompiled header files nemed something like "_Prefix.pch". To avoid such problems - delete the #import statement or - insert an #import statement above the cocoa.h import statement

即使您将“main.m”重命名为“main.mm”并将“#include opencv2/opencv.hpp”移动到顶部(在主文件中),由于预编译,预处理器也会首先插入 cocoa.h头文件类似于“_Prefix.pch”。为避免此类问题 - 删除 #import 语句或 - 在 cocoa.h import 语句上方插入 #import 语句

回答by petert

Try adding: -lstdc++to the "Other linker flags" in the build settings for your Cocoa app.

尝试将:添加-lstdc++到 Cocoa 应用程序构建设置中的“其他链接器标志”。

A cocoa application made by the Xcode templates won't link include the c++ library in it's settings by default.

默认情况下,由 Xcode 模板制作的可可应用程序不会在其设置中链接包含 c++ 库。

回答by Mehul Thakkar

  1. add this to your .pch file

    #ifdef __cplusplus
    #import <opencv2/opencv.hpp>
    #endif
    
  2. dont forget to convert all of your .m files into .mm files

  1. 将此添加到您的 .pch 文件中

    #ifdef __cplusplus
    #import <opencv2/opencv.hpp>
    #endif
    
  2. 不要忘记将所有 .m 文件转换为 .mm 文件

回答by Paul R

You'll probably find it easier to use OpenCV's CAPI rather than the C++API. You can call C APIs directly from Objective C of course, so this makes life a lot easier. You just lose some of C++'s syntactic sugar, like default parameter values.

您可能会发现使用 OpenCV 的CAPI 比使用API更容易C++。当然,您可以直接从 Objective C 调用 C API,因此这让生活变得更加轻松。你只是失去了一些 C++ 的语法糖,比如默认参数值。