使用 Xcode 为 OS X Lion / Mountain Lion 编译 OpenCV (2.3.1+)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8552839/
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
Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode
提问by dom
Can anyone provide me some detailed guide how to compile OpenCV 2.3.1 on OS X Lion with Xcode?
谁能为我提供一些如何在 OS X Lion 上使用 Xcode 编译 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
提前致谢,多姆
SOLUTIONin my answer post.
解决方案在我的回答帖子中。
回答by dom
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. 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:使用 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.
204年 10 月 16 日编辑:去年我放弃了 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(截至 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.) 选择您的目标,转到“常规”并点击“链接的框架和库”中的“+”按钮
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. 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 保持最新状态:
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/”添加到“构建设置”下的“标题搜索路径”(目标仍被选中)
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 :)
就是这样!希望能帮助到你 :)
回答by b0ts
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,我能够在 Mavericks 上运行 OpenCv。我以稍微不同的顺序做事,所以这是我的笔记。
If it isn't installed, install Macports and add it to path. (I already had it installed)
Use Macports to update itself from the command line $ sudo port -v selfupdate
Use Macports to install opencv and its dependencies from the command line. $ sudo port install opencv
If it isn't installed, install xCode. (I already had it installed)
Use xCode to create a C++ Command line project.
Use xCode to verify that the simple hello world C++ program it created works by running it in the xCode IDE.
Modify the main.cpp file created by xCode from the hello world example to the simple opencv example from Vachidrewer.
Notice that xCode editer reports that it can NOT find the header opencv header file.
Add /opt/local/include/ to the project search path and notice that the editer errors go away.
Try to run the program in the xCode IDE and notice that it reports that it can't find the opencv libraries.
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.
Use the xCode IDE to run the project and notice that a window pops up with half od it darker then the other half.
如果未安装,请安装 Macports 并将其添加到路径中。(我已经安装了)
使用 Macports 从命令行更新自身 $ sudo port -v selfupdate
使用 Macports 从命令行安装 opencv 及其依赖项。$ sudo 端口安装opencv
如果未安装,请安装 xCode。(我已经安装了)
使用 xCode 创建 C++ 命令行项目。
使用 xCode 通过在 xCode IDE 中运行它来验证它创建的简单 hello world C++ 程序是否有效。
将 xCode 创建的 main.cpp 文件从 hello world 示例修改为 Vachidrewer 中的简单 opencv 示例。
请注意,xCode 编辑器报告它找不到头文件 opencv 头文件。
将 /opt/local/include/ 添加到项目搜索路径并注意编辑器错误消失了。
尝试在 xCode IDE 中运行该程序,并注意到它报告找不到 opencv 库。
在项目中添加一个名为 opencvfrqmework 的组,并将 /opt/local/lib/libopencv_core.dylib 和 /opt/local/lib/livopencv_highgui.dylib 添加到该组中。
使用 xCode IDE 运行该项目,并注意到弹出一个窗口,其中一半比另一半更暗。
回答by vaichidrewar
With small changes to @moosgummi answer below steps worked with Xcode 4.6on Mac OSX 10.7TEST 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 保持最新状态:
sudo port -v selfupdate
Install OpenCV with mac ports
使用 mac 端口安装 OpenCV
sudo port install opencv
Configuring Xcode for using OpenCV
配置 Xcode 以使用 OpenCV
Create a new XCode project using the Command Line Utility/Standard Tool template. Name it and select C++
Select Project -> Edit Project Settings. Select the Build tab. Set Configuration to All Configurations
In the Architectures section, double-click Valid Architectures and remove all the PPC architectures if any.
Compiler for C/C++/Objective-C > Apple LLVM compiler 4.2 Language" > "C++ Standard Library", and select "libstdc++ (GNU C++ standard library)"
In the Search Paths section set Header Search Paths to /opt/local/include/
Please choose non-recursive as an option while adding that search pathClose the Project Info window
Select Project -> New Group and create a group called OpenCV Frameworks With the new group selected, select Project -> Add files to 'Your Project Name'
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.)
Uncheck Copy Items… and click Add
使用命令行实用程序/标准工具模板创建一个新的 XCode 项目。命名并选择 C++
选择项目 -> 编辑项目设置。选择构建选项卡。将配置设置为所有配置
在架构部分,双击有效架构并删除所有 PPC 架构(如果有)。
Compiler for C/C++/Objective-C > Apple LLVM compiler 4.2 Language” > “C++ Standard Library”,选择“libstdc++ (GNU C++ standard library)”
在搜索路径部分将标题搜索路径设置为 /opt/local/include/
请在添加该搜索路径时选择非递归作为选项关闭项目信息窗口
选择 Project -> New Group 并创建一个名为 OpenCV Frameworks 的组选择新组后,选择 Project -> Add files to 'Your Project Name'
按“/”键获得转到文件夹提示。输入/opt/local/lib 选择libopencv_core.dylib、libopencv_highgui.dylib(你可能需要从这个文件夹中添加其他库文件来运行其他代码。)
取消选中复制项目...,然后单击添加
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;
}
回答by mevatron
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 buildin section 2.
按照本指南,看看是否有帮助。如果您已经下载了源代码,您可能不需要执行建议的 svn checkout。您可能可以从使用 CMake开始在第 2 节中进行构建。
Hope that helps.
希望有帮助。
回答by Guilherme Defreitas
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