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

时间:2021-04-13 16:45:24

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.

答案就在我的回答中。

5 个解决方案

#1


15  

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在OS X Lion(10.7.2)下运行OpenCV 2.3.1,并运行Xcode 4.2.1。

EDIT 08/06/2012: This is also working for 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).

编辑08/06/2012:这也适用于OpenCV 2.4.1。只要确保您得到了最新版本的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:在Xcode和OpenCV的当前版本中,使用Mountain Lion测试了所有的东西,并且你不需要使用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 doctor && brew update 

3.) Install OpenCV (as of 17/20/2014 v2.4.9)

3)。安装OpenCV (17/20/2014 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(从17/20/2014 v6.0.1开始)并打开/创建项目

5.) Select your target, go to "General" and hit the "+"-Button in the "Linked Frameworks and Libraries"

5)。选择您的目标,转到“General”并在“链接框架和库”中单击“+”-按钮

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" (target still selected)

7.) Finally make sure include OpenCV in your .mm files.

7)。最后,确保在.mm文件中包含OpenCV。

MacPorts guide (maybe outdated)

1.) Get the current Version of MacPorts here. Don't forget to add "/opt/local/(s)bin" to your environment PATH

1)。在这里获取MacPorts的当前版本。不要忘记在您的环境路径中添加“/opt/local/(s)bin”

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

2.) Keep your MacPorts up-2-date:

2)。保持你的MacPorts up-2-date:

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)。选择您的目标,进入“Summary”,在“Linked Frameworks和Libraries”点击“+”按钮

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" (target still selected)

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:

否则你会得到一些像这样讨厌的erros:

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

That's it! Hope it helps :)

就是这样!希望它能帮助:)

#2


2  

Thanks to Vachidrewer, I was able to get OpenCv running on Mavericks. I did things in a little different order,so here are my notes.

多亏了Vachidrewer,我才能让OpenCv在小牛上奔跑。我做的顺序有点不同,这是我的笔记。

  1. If it isn't installed, install Macports and add it to path. (I already had it installed)

    如果没有安装,请安装Macports并将其添加到path中。(我已经安装了)

  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. $ sudo port install opencv

    使用Macports从命令行安装opencv及其依赖项。$ sudo端口安装opencv

  4. If it isn't installed, install xCode. (I already had it installed)

    如果没有安装,请安装xCode。(我已经安装了)

  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来验证这个简单的hello world c++程序,它是通过在xCode IDE中运行它来创建工作的。

  7. Modify the main.cpp file created by xCode from the hello world example to the simple opencv example from Vachidrewer.

    修改主。xCode从hello world示例创建的cpp文件到Vachidrewer简单的opencv示例。

  8. Notice that xCode editer reports that it can NOT find the header opencv header file.

    注意,xCode编辑器报告它找不到头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 /地方/ 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来运行项目,注意窗口会弹出一个窗口,窗口的颜色会比窗口的颜色深一半。

#3


1  

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. You can probably start with Use CMake to build in section 2.

遵循这个指南,看看是否有帮助。如果您已经下载了源代码,那么您可能不需要执行所建议的svn检查。您可以从第2节中使用CMake构建开始。

Hope that helps.

希望有帮助。

#4


1  

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的步骤。

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 up-2-date:

sudo port -v selfupdate

Install OpenCV with mac ports

使用mac端口安装OpenCV

sudo port install opencv

Configuring Xcode for using OpenCV

配置使用OpenCV的Xcode

  1. Create a new XCode project using the Command Line Utility/Standard Tool template. Name it and select C++

    使用命令行实用程序/标准工具模板创建一个新的XCode项目。命名它并选择c++

  2. Select Project -> Edit Project Settings. Select the Build tab. Set Configuration to All Configurations

    选择Project ->编辑项目设置。选择构建选项卡。将配置设置为所有配置

  3. In the Architectures section, double-click Valid Architectures and remove all the PPC architectures if any.

    在体系结构部分,双击有效体系结构并删除所有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 >苹果LLVM编译器4.2语言“>”c++标准库,选择“libstdc++ ++ (GNU c++标准库)”

  5. In the Search Paths section set Header Search Paths to /opt/local/include/
    Please choose non-recursive as an option while adding that search path

    在“搜索路径”一节中,设置了/opt/local/include/请在添加搜索路径时选择非递归的搜索路径

  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 framework的组,选择New Group,选择Project ->向“您的项目名称”添加文件

  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 /地方/ lib选择libopencv_core。dylib libopencv_highgui。dylib(您可能需要从这个文件夹中添加其他库文件来运行其他代码)。

  9. Uncheck Copy Items… and click Add

    取消选中复制项……并单击Add

TEST CODE

测试代码

Copy this code into your main.cpp file. It should open up a small window that is half shaded.

将此代码复制到您的main中。cpp文件。它应该打开一个有一半阴影的小窗口。

#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;
}

#5


-1  

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

#1


15  

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在OS X Lion(10.7.2)下运行OpenCV 2.3.1,并运行Xcode 4.2.1。

EDIT 08/06/2012: This is also working for 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).

编辑08/06/2012:这也适用于OpenCV 2.4.1。只要确保您得到了最新版本的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:在Xcode和OpenCV的当前版本中,使用Mountain Lion测试了所有的东西,并且你不需要使用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 doctor && brew update 

3.) Install OpenCV (as of 17/20/2014 v2.4.9)

3)。安装OpenCV (17/20/2014 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(从17/20/2014 v6.0.1开始)并打开/创建项目

5.) Select your target, go to "General" and hit the "+"-Button in the "Linked Frameworks and Libraries"

5)。选择您的目标,转到“General”并在“链接框架和库”中单击“+”-按钮

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" (target still selected)

7.) Finally make sure include OpenCV in your .mm files.

7)。最后,确保在.mm文件中包含OpenCV。

MacPorts guide (maybe outdated)

1.) Get the current Version of MacPorts here. Don't forget to add "/opt/local/(s)bin" to your environment PATH

1)。在这里获取MacPorts的当前版本。不要忘记在您的环境路径中添加“/opt/local/(s)bin”

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

2.) Keep your MacPorts up-2-date:

2)。保持你的MacPorts up-2-date:

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)。选择您的目标,进入“Summary”,在“Linked Frameworks和Libraries”点击“+”按钮

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" (target still selected)

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:

否则你会得到一些像这样讨厌的erros:

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

That's it! Hope it helps :)

就是这样!希望它能帮助:)

#2


2  

Thanks to Vachidrewer, I was able to get OpenCv running on Mavericks. I did things in a little different order,so here are my notes.

多亏了Vachidrewer,我才能让OpenCv在小牛上奔跑。我做的顺序有点不同,这是我的笔记。

  1. If it isn't installed, install Macports and add it to path. (I already had it installed)

    如果没有安装,请安装Macports并将其添加到path中。(我已经安装了)

  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. $ sudo port install opencv

    使用Macports从命令行安装opencv及其依赖项。$ sudo端口安装opencv

  4. If it isn't installed, install xCode. (I already had it installed)

    如果没有安装,请安装xCode。(我已经安装了)

  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来验证这个简单的hello world c++程序,它是通过在xCode IDE中运行它来创建工作的。

  7. Modify the main.cpp file created by xCode from the hello world example to the simple opencv example from Vachidrewer.

    修改主。xCode从hello world示例创建的cpp文件到Vachidrewer简单的opencv示例。

  8. Notice that xCode editer reports that it can NOT find the header opencv header file.

    注意,xCode编辑器报告它找不到头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 /地方/ 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来运行项目,注意窗口会弹出一个窗口,窗口的颜色会比窗口的颜色深一半。

#3


1  

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. You can probably start with Use CMake to build in section 2.

遵循这个指南,看看是否有帮助。如果您已经下载了源代码,那么您可能不需要执行所建议的svn检查。您可以从第2节中使用CMake构建开始。

Hope that helps.

希望有帮助。

#4


1  

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的步骤。

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 up-2-date:

sudo port -v selfupdate

Install OpenCV with mac ports

使用mac端口安装OpenCV

sudo port install opencv

Configuring Xcode for using OpenCV

配置使用OpenCV的Xcode

  1. Create a new XCode project using the Command Line Utility/Standard Tool template. Name it and select C++

    使用命令行实用程序/标准工具模板创建一个新的XCode项目。命名它并选择c++

  2. Select Project -> Edit Project Settings. Select the Build tab. Set Configuration to All Configurations

    选择Project ->编辑项目设置。选择构建选项卡。将配置设置为所有配置

  3. In the Architectures section, double-click Valid Architectures and remove all the PPC architectures if any.

    在体系结构部分,双击有效体系结构并删除所有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 >苹果LLVM编译器4.2语言“>”c++标准库,选择“libstdc++ ++ (GNU c++标准库)”

  5. In the Search Paths section set Header Search Paths to /opt/local/include/
    Please choose non-recursive as an option while adding that search path

    在“搜索路径”一节中,设置了/opt/local/include/请在添加搜索路径时选择非递归的搜索路径

  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 framework的组,选择New Group,选择Project ->向“您的项目名称”添加文件

  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 /地方/ lib选择libopencv_core。dylib libopencv_highgui。dylib(您可能需要从这个文件夹中添加其他库文件来运行其他代码)。

  9. Uncheck Copy Items… and click Add

    取消选中复制项……并单击Add

TEST CODE

测试代码

Copy this code into your main.cpp file. It should open up a small window that is half shaded.

将此代码复制到您的main中。cpp文件。它应该打开一个有一半阴影的小窗口。

#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;
}

#5


-1  

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