简体   繁体   English

使用Xcode为OS X Lion / Mountain Lion编译OpenCV(2.3.1+)

[英]Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode

Can anyone provide me some detailed guide how to compile OpenCV 2.3.1 on OS X Lion with Xcode? 任何人都可以为我提供一些如何使用Xcode在OS X Lion上编译OpenCV 2.3.1的详细指南吗?

I'm getting mad about this … I got the source, used cmake to create the Xcode template and tried to build it, but it fails with about 200 errors. 我对此感到生气...我得到了源码,使用cmake创建Xcode模板并尝试构建它,但它失败了大约200个错误。

Thanks in advance, Dom 在此先感谢,Dom

SOLUTION in my answer post. 解答我的回答帖子。

Detailed guide how to get OpenCV 2.3.1 up and running under OS X Lion (10.7.2) with Xcode 4.2.1 using MacPorts 详细指南如何使用MacPorts在Xcode 4.2.1的OS X Lion(10.7.2)下启动和运行OpenCV 2.3.1

EDIT 08/06/2012: This is also working for OpenCV 2.4.1. 编辑08/06/2012:这也适用于OpenCV 2.4.1。 Just make sure you got the latest version of Xcode and installed the "Command Line Tools" (Xcode -> Preferences -> Downloads -> Command Line Tools). 只需确保您获得最新版本的Xcode并安装“命令行工具”(Xcode - >首选项 - >下载 - >命令行工具)。

EDIT 15/08/2012: Tested everything with Mountain Lion ans the current versions of Xcode & OpenCV … it's working :) And you don't have to use the LLVM compiler. 编辑15/08/2012:使用Mountain Lion和当前版本的Xcode和OpenCV测试了所有内容...它正在运行:)而且您不必使用LLVM编译器。

EDIT 16/10/204: Over the last year I abandoned MacPorts and started using brew, which works better for my purposes. 编辑16/10/204:在过去的一年里,我放弃了MacPorts并开始使用brew,这对我的目的更有效。

Brew guide 酿造指南

1.) Get the current Version of Brew here . 1.)在这里获取当前版本的Brew。

2.) Make sure brew is ready to us 2.)确保brew已经准备好了

brew doctor && brew update 

3.) Install OpenCV (as of 17/20/2014 v2.4.9) 3.)安装OpenCV(截至2014年7月20日v2.4.9)

brew install opencv

4.) Fire up Xcode (as of 17/20/2014 v6.0.1) and open/create your project 4.)启动Xcode(截至2014年7月17日v6.0.1)并打开/创建您的项目

5.) Select your target, go to "General" and hit the "+"-Button in the "Linked Frameworks and Libraries" 5.)选择目标,转到“常规”并点击“链接的框架和库”中的“+” - 按钮

5.1.) Click "Add Other", hit "/", go to "/usr/local/lib" and add any libopencv_**.dylib you need 5.1。)点击“添加其他”,点击“/”,转到“/ usr / local / lib”并添加你需要的任何libopencv _ **。dylib

6.) Now add "/usr/local/include" to your "Header Search Paths" under "Build Settings" (target still selected) 6.)现在将“/ usr / local / include”添加到“Build Settings”下的“Header Search Paths”(目标仍然被选中)

7.) Finally make sure include OpenCV in your .mm files. 7.)最后确保在.mm文件中包含OpenCV。

MacPorts guide (maybe outdated) MacPorts指南(可能已过时)

1.) Get the current Version of MacPorts here . 1.)在此处获取当前版本的MacPorts。 Don't forget to add "/opt/local/(s)bin" to your environment PATH 不要忘记将“/ opt / local /(s)bin”添加到您的环境路径中

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

2.) Keep your MacPorts up-2-date: 2.)保持您的MacPorts 2日期:

sudo port -v selfupdate

3.) Install OpenCV 2.3.1 (building with llvm-gcc) 3.)安装OpenCV 2.3.1(使用llvm-gcc构建)

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

4.) Fire up Xcode and create your project 4.)启动Xcode并创建项目

5.) Select your target, go to "Summary" and hit the "+"-Button in the "Linked Frameworks and Libraries" 5.)选择目标,转到“摘要”并点击“链接的框架和库”中的“+” - 按钮

5.1.) Click "Add Other", hit "/" and go to "/opt/local/lib" 5.1。)点击“添加其他”,点击“/”然后转到“/ opt / local / lib”

5.2.) Add any libopencv_**.dylib you need 5.2。)添加你需要的任何libopencv _ **。dylib

6.) Now add "/opt/local/include/" to your "Header Search Paths" under "Build Settings" (target still selected) 6.)现在将“/ opt / local / include /”添加到“Build Settings”下的“Header Search Paths”(目标仍然被选中)

7.) Finally make sure to have the following lines at the beginning of your .pch file: 7.)最后确保在.pch文件的开头有以下几行:

#ifdef __cplusplus
  #import "opencv2/opencv.hpp"
#endif

Otherwise you'll get some nasty erros like this: 否则你会得到一些令人讨厌的错误:

"Non-const static data member must be initialized out of line"
"Statement expression not allowed at file scope"

That's it! 而已! Hope it helps :) 希望能帮助到你 :)

Thanks to Vachidrewer, I was able to get OpenCv running on Mavericks. 感谢Vachidrewer,我能够在Mavericks上运行OpenCv。 I did things in a little different order,so here are my notes. 我按照不同的顺序做事,所以这是我的笔记。

  1. If it isn't installed, install Macports and add it to path. 如果未安装,请安装Macport并将其添加到路径。 (I already had it installed) (我已经安装了它)

  2. Use Macports to update itself from the command line $ sudo port -v selfupdate 使用Macports从命令行$ sudo port -v selfupdate更新自己

  3. Use Macports to install opencv and its dependencies from the command line. 使用Macports从命令行安装opencv及其依赖项。 $ sudo port install opencv $ sudo port install opencv

  4. If it isn't installed, install xCode. 如果未安装,请安装xCode。 (I already had it installed) (我已经安装了它)

  5. Use xCode to create a C++ Command line project. 使用xCode创建C ++命令行项目。

  6. Use xCode to verify that the simple hello world C++ program it created works by running it in the xCode IDE. 使用xCode通过在xCode IDE中运行它来验证它创建的简单hello world C ++程序是否有效。

  7. Modify the main.cpp file created by xCode from the hello world example to the simple opencv example from Vachidrewer. 将xCode创建的main.cpp文件从hello world示例修改为Vachidrewer的简单opencv示例。

  8. Notice that xCode editer reports that it can NOT find the header opencv header file. 请注意,xCode editer报告它无法找到头文件opencv头文件。

  9. Add /opt/local/include/ to the project search path and notice that the editer errors go away. 将/ opt / local / include /添加到项目搜索路径,并注意编辑器错误消失。

  10. Try to run the program in the xCode IDE and notice that it reports that it can't find the opencv libraries. 尝试在xCode IDE中运行该程序,并注意它报告它找不到opencv库。

  11. Add a group called opencvfrqmework to the project and add /opt/local/lib/libopencv_core.dylib and /opt/local/lib/livopencv_highgui.dylib to the group. 将一个名为opencvfrqmework的组添加到项目中,并将/opt/local/lib/libopencv_core.dylib和/opt/local/lib/livopencv_highgui.dylib添加到该组。

  12. Use the xCode IDE to run the project and notice that a window pops up with half od it darker then the other half. 使用xCode IDE运行项目,并注意到一个窗口弹出一半,然后是另一半。

With small changes to @moosgummi answer below steps worked with Xcode 4.6 on Mac OSX 10.7 TEST code is included below. 随着@moosgummi的小改动回答下面的步骤使用Mac OSX 10.7上的Xcode 4.6 ,下面包括TEST代码。

Installation of OpenCV : 安装OpenCV:

Get the current Version of MacPorts here . 此处获取当前版本的MacPorts。

Don't forget to add "/opt/local/(s)bin" to your environment PATH 不要忘记将“/ opt / local /(s)bin”添加到您的环境路径中

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

Keep your MacPorts up-2-date: 让您的MacPorts保持2个日期:

sudo port -v selfupdate

Install OpenCV with mac ports 使用mac端口安装OpenCV

sudo port install opencv

Configuring Xcode for using OpenCV 配置Xcode以使用OpenCV

  1. Create a new XCode project using the Command Line Utility/Standard Tool template. 使用命令行实用程序/标准工具模板创建新的XCode项目。 Name it and select C++ 将其命名并选择C ++

  2. Select Project -> Edit Project Settings. 选择项目 - >编辑项目设置。 Select the Build tab. 选择“构建”选项卡。 Set Configuration to All Configurations 将配置设置为所有配置

  3. In the Architectures section, double-click Valid Architectures and remove all the PPC architectures if any. 在Architectures部分中,双击Valid Architectures并删除所有PPC体系结构(如果有)。

  4. Compiler for C/C++/Objective-C > Apple LLVM compiler 4.2 Language" > "C++ Standard Library", and select "libstdc++ (GNU C++ standard library)" 编译器用于C / C ++ / Objective-C> Apple LLVM编译器4.2语言“>”C ++标准库“,并选择”libstdc ++(GNU C ++标准库)“

  5. In the Search Paths section set Header Search Paths to /opt/local/include/ 在“搜索路径”部分中,将标题搜索路径设置为/ opt / local / include /
    Please choose non-recursive as an option while adding that search path 添加该搜索路径时,请选择非递归作为选项

  6. Close the Project Info window 关闭“项目信息”窗口

  7. Select Project -> New Group and create a group called OpenCV Frameworks With the new group selected, select Project -> Add files to 'Your Project Name' 选择Project - > New Group并创建一个名为OpenCV Frameworks的组在选择新组的情况下,选择Project - >将文件添加到'Your Project Name'

  8. Press the "/" key to get the Go to the folder prompt. 按“/”键进入“转到文件夹”提示。 Enter /opt/local/lib Select libopencv_core.dylib, libopencv_highgui.dylib (you may need to add other library files from this folder to run other code.) 输入/ opt / local / lib选择libopencv_core.dylib,libopencv_highgui.dylib(您可能需要从此文件夹添加其他库文件以运行其他代码。)

  9. Uncheck Copy Items… and click Add 取消选中“复制项目...”,然后单击“添加”

TEST CODE 测试代码

Copy this code into your main.cpp file. 将此代码复制到main.cpp文件中。 It should open up a small window that is half shaded. 它应该打开一个半阴影的小窗口。

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char *argv[])
{
    // Open the file.

    IplImage *img = cvCreateImage( cvSize(100,200), IPL_DEPTH_8U, 3); //if (!img) {

    //    printf("Error: Couldn't open the image file.\n");
    //    return 1;
    //}

    // Display the image.
    cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
    cvShowImage("Image:", img);

    // Wait for the user to press a key in the GUI window.
    cvWaitKey(0);
    // Free the resources.
    cvDestroyWindow("Image:");
    cvReleaseImage(&img);

    return 0;
}

Have you tried just building it using standard UNIX Makefiles? 您是否尝试使用标准UNIX Makefile构建它?

Follow this guide , and see if that helps. 请遵循本指南 ,看看是否有帮助。 If you have already downloaded the source code, you probably don't need to do the svn checkout that is suggested. 如果您已经下载了源代码,则可能不需要执行建议的svn checkout。 You can probably start with Use CMake to build in section 2. 您可以从使用CMake开始构建第2部分。

Hope that helps. 希望有所帮助。

After get a lot of errors (segmentation fault etc) I finally get it working. 在得到很多错误(分段错误等)之后我终于开始工作了。 Here is the tutorial: 这是教程:

http://www.guidefreitas.com/installing-opencv-2-4-2-on-mac-osx-mountain-lion-with-python-support http://www.guidefreitas.com/installing-opencv-2-4-2-on-mac-osx-mountain-lion-with-python-support

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM