如何将 OpenCV 包含到 XCode 4 c++ 项目中?

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

How to include OpenCV include into XCode 4 c++ project?

c++xcodemacosopencvxcode4.2

提问by Sefu

I have seen posts on this before, but none of the answers seemed to work for me. I just installed OpenCV on my Mac and got it to work fine with g++ from the command line. I installed it using Mac Ports:

我以前看过这方面的帖子,但似乎没有一个答案对我有用。我刚刚在我的 Mac 上安装了 OpenCV,并让它在命令行中与 g++ 一起正常工作。我使用 Mac Ports 安装它:

$ sudo port install opencv configure.compiler=llvm-gcc-4.2

To compile with g++, I use the command:

要使用 g++ 进行编译,我使用以下命令:

g++ myfile.cpp -o myprogram -I/opt/local/include -L/opt/local/lib -lopencv_core.2.4.2 -lopencv_calib3d.2.4.2

And my OpenCV include header in the main.cpp file is:

我的 OpenCV 在 main.cpp 文件中包含头文件是:

#include "opencv2/opencv.hpp"

And all works well.

一切正常。

Now, when I tried to include the library in Xcode, it simply says 'opencv2/opencv.hpp' file not found. To include the library in Xcode, I followed some detailed instructions. First, to the project target, I added /opt/local/lib to the library search paths, and /opt/local/include, /opt/local/include/opencv and /opt/local/include/opencv2 to the header search paths under the Build Settings tab. Then I clicked on Build Phases, and to Link Binary With Libraries I added all of the OpenCV .dylib files in /opt/local/lib (all 11 of them). This didn't work. I've tried many other things, including adding to user search paths and setting up "Other Linker Flags" for the target. Nothing worked. It can't find the file. If I use the exact path (#include "/opt/local/include/opencv2/opencv.hpp") it can't find all the other header files.

现在,当我尝试在 Xcode 中包含该库时,它只是说找不到“opencv2/opencv.hpp”文件。为了将库包含在 Xcode 中,我遵循了一些详细说明。首先,对于项目目标,我将 /opt/local/lib 添加到库搜索路径,并将 /opt/local/include、/opt/local/include/opencv 和 /opt/local/include/opencv2 添加到标头搜索构建设置选项卡下的路径。然后我单击 Build Phases,并将 Binary With Libraries 添加到 /opt/local/lib(全部 11 个)中的所有 OpenCV .dylib 文件。这没有用。我尝试了很多其他的事情,包括添加到用户搜索路径和为目标设置“其他链接器标志”。没有任何效果。它找不到文件。如果我使用确切的路径(#include“/opt/local/include/opencv2/opencv.hpp”)它可以'

The version of OpenCV is 2.4.2. I am using Xcode 4.3.3 on OS X Lion 10.7.4. This is a C++ project. Any ideas?

OpenCV 的版本是 2.4.2。我在 OS X Lion 10.7.4 上使用 Xcode 4.3.3。这是一个 C++ 项目。有任何想法吗?

回答by Glenn

This problem can be solved by going to the project properties and click on the target. In the build settings search "header search path". And there you add "/opt/local/include". By doing this you're telling Xcode to look for the header files in that directory also. Now you can use:

这个问题可以通过进入项目属性并点击目标来解决。在构建设置中搜索“标题搜索路径”。然后添加“/opt/local/include”。通过这样做,您也告诉 Xcode 在该目录中查找头文件。现在您可以使用:

#include <opencv2/opencv.hpp>

#include <opencv2/opencv.hpp>

If you don't want to do this every time you start a new project you can add the .h and .hpp files to the defaulti include directory Xcode uses. You can find this directory in Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include .

如果您不想在每次启动新项目时都这样做,您可以将 .h 和 .hpp 文件添加到 Xcode 使用的默认包含目录中。您可以在 Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include 中找到此目录。

You will have to add the binaries to your project as well. For that you go to "build phases" and then add the binaries in the "Link binaries with libraries" section.

您还必须将二进制文件添加到您的项目中。为此,您转到“构建阶段”,然后在“将二进制文件与库链接”部分中添加二进制文件。

I hope this helps you out.

我希望这能够帮到你。