mac 安裝opencv homebrew安裝
0 Homebrew是啥?
“Homebrew installs the stuff you need that Apple didn’t.——Homebrew 使 OS X 更完整”。
Homebrew的官網[1](多語言版本)簡單明瞭地介紹瞭如何安裝和使用這個工具,並提供了自己的Wiki。
1 安裝Homebrew
brew的安裝很簡單,使用一條ruby命令即可,Mac系統上已經預設安裝了ruby。
gerryyang@mba:bin$ruby
--version
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
關於安裝brew的詳細說明可以參考其github上的wiki說明[2]:
"
Installation
The suggested and easiest way to install Homebrew is on the homepage. We don’t duplicate it here because it is asecurity risk to list it on a user-editable wiki.
The standard script installs Homebrew to /usr/local
so that you don’t
need sudo when youbrew install
. It is acareful script, it can be run even if you have stuff installed to/usr/local
already. It tells you exactly what it will do before itdoes it too. And you have to confirm everything it will
do before itstarts.
There are other ways to install Homebrew which provide you with moreflexibility. They are listed below the requirements.
"- gerryyang@mba:~$ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
- It appears Homebrew is already installed. If your intent is to reinstall you
- should do the following before running this installer again:
- rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup
由於我的系統上已經安裝過brew,因此再次執行安裝命令會提示我brew已經存在。
2 Homebrew的用法
檢視brew的具體用法:
- gerryyang@mba:~$brew
- Example usage:
- brew [info | home | options ] [FORMULA...]
- brew install FORMULA...
- brew uninstall FORMULA...
- brew search [foo]
- brew list [FORMULA...]
- brew update
- brew upgrade [FORMULA...]
- brew pin/unpin [FORMULA...]
- Troubleshooting:
- brew doctor
- brew install -vd FORMULA
- brew [--env | --config]
- Brewing:
- brew create [URL [--no-fetch]]
- brew edit [FORMULA...]
- open https://github.com/Homebrew/homebrew/wiki/Formula-Cookbook
- Further help:
- man brew
- brew home
- gerryyang@mba:~$
在安裝好brew後,只需要一條命令就可以安裝OpenCV了:
通常情況下這樣做就應該會安裝成功,但我在公司和家裡面的電腦嘗試的時候,brew都會報一些錯誤,我遇到的都是一些小問題,按照brew的提示資訊,解決掉相應的問題即可。
安裝成功後,你應該可以在“/usr/local/include”目錄下找到名為opencv和opencv2的目錄,這裡面是OpenCV相關的標頭檔案。你也可以在“/usr/local/lib”目錄下找到許多以libopencv_開頭的.dylib檔案,這些是OpenCV的連結庫檔案。
這裡有一個技巧,因為 /usr 目錄在對話方塊中預設不是可見的,可以按快捷鍵 command + shift + G,在彈出的“前往資料夾”對話方塊中輸入 /usr/local/lib ,即可跳轉到目標資料夾。如下圖所示:
下一步是我自己試出來的,對於Lion作業系統,你需要在Build Settings中,將“C++ Language Dialect”設定成C++11,將“C++ Standard Library”設定成libstdc++ ,如下圖所示。個人感覺是由於XCode預設設定的GNU++11、libc++與OpenCV庫有一些相容性問題,我在更改該設定前老是出現編譯錯誤。後續版本在Montain Lion系統中解決了這個問題,不用進行這一步了。
把上面的設定都做好後,就可以在需要的使用OpenCV庫的地方,加上opencv的標頭檔案引用即可:
注意,如果你的原始檔副檔名是.m的,你還需要改成.mm,這樣編譯器才知道你將會在該檔案混合使用C++語言和Objective-C語言。
OpenCV處理圖象需要的格式是cv::Mat類,而MacOS的圖象格式預設是NSImage,所以你需要知道如何在cv::Mat與NSImage之前相互轉換。如下是一個NSImage的Addition,你肯定會需要它的。該程式碼來自stackoverflow上的這個貼子。
NSImage+OpenCV.h 檔案:
NSImage+OpenCV.mm檔案:
完成以上步驟後,恭喜你,你可以在原始碼中自由地呼叫OpenCV的函式了。
3 Homebrew的有趣問題
下面關於Homebrew的一些常見問題[3]:
(1) 在OS X中找不到想要的軟體,可以使用brew安裝;
- gerryyang@mba:~$brew install wget
- ==> Downloading http://ftpmirror.gnu.org/wget/wget-1.14.tar.gz
- ######################################################################## 100.0%
- ==> ./configure --prefix=/usr/local/Cellar/wget/1.14 --sysconfdir=/usr/local/etc
- ==> make install
- Warning: Could not link wget. Unlinking...
- Error: The `brew link` step did not complete successfully
- The formula built, but is not symlinked into /usr/local
- You can try again using `brew link wget'
- Possible conflicting files are:
- /usr/local/bin/wget
- /usr/local/share/man/man1/wget.1
- ==> Summary
- <span style="font: 13.0px Monaco">/usr/local/Cellar/wget/1.14: 8 files, 688K, built in 2.3 minutes</span>
由於之前使用原始碼方式安裝過wget,因此再用brew安裝時會提示衝突的錯誤,因為/usr/locat/bin/wget已經存在。實際上,Homebrew 將軟體包分裝到單獨的目錄(/usr/local/Cellar),然後
symlink 到 /usr/local 中。
(2) 如何更新本地已安裝的package呢?
第一步:先更新brew:
brew update
- gerryyang@mba:bin$brew update
- Already up-to-date.
第二步:更新所有package:
brew upgarde
- gerryyang@mba:bin$brew upgrade
- ==> Upgrading 1 outdated package, with result:
- wget 1.15
- ==> Upgrading wget
- ==> Installing wget dependency: openssl
- ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0
- ######################################################################## 100.0%
- ==> Pouring openssl-1.0.1f.mavericks.bottle.tar.gz
- ==> Caveats
- This formula is keg-only, so it was not symlinked into /usr/local.
- Mac OS X already provides this software and installing another version in
- parallel can cause all kinds of trouble.
- The OpenSSL provided by OS X is too old for some software.
- Generally there are no consequences of this for you. If you build your
- own software and it requires this formula, you'll need to add to your
- build variables:
- LDFLAGS: -L/usr/local/opt/openssl/lib
- CPPFLAGS: -I/usr/local/opt/openssl/include
- ==> security find-certificate -a -p /Library/Keychains/System.keychain > '/usr/l
- ==> security find-certificate -a -p /System/Library/Keychains/SystemRootCertific
- ==> mv -f /usr/local/etc/openssl/osx_cert.pem.tmp /usr/local/etc/openssl/osx_cer
- ==> Summary
- /usr/local/Cellar/openssl/1.0.1f: 429 files, 15M
- ==> Installing wget
- ==> Downloading http://ftpmirror.gnu.org/wget/wget-1.15.tar.gz
- ######################################################################## 100.0%
- ==> ./configure --prefix=/usr/local/Cellar/wget/1.15 --sysconfdir=/usr/local/etc --with-ssl=openssl --with-libssl-prefix=/usr/local/opt/openssl --disable-iri
- ==> make install
- Warning: Could not link wget. Unlinking...
- Error: The `brew link` step did not complete successfully
- The formula built, but is not symlinked into /usr/local
- You can try again using `brew link wget'
- Possible conflicting files are:
- /usr/local/bin/wget
- /usr/local/share/man/man1/wget.1
- ==> Summary
- /usr/local/Cellar/wget/1.15: 8 files, 700K, built in 73 seconds
- gerryyang@mba:bin$
(3) 如何刪除Homebrew?
If you installed to /usr/local
then you can use the script in thisgist to uninstall — it will onlyremove Homebrew and
the stuff Homebrew installed leaving anything elsein/usr/local
alone.
Provided you haven’t put anything else in Homebrew’s prefix(brew --prefix
), you can generally justrm -rf
that directory. Thisis because Homebrew won’t touch files outside its prefix.
(4) Homebrew下載的package存放的路徑在哪裡?
- gerryyang@mba:bin$brew --cache
- /Library/Caches/Homebrew
- gerryyang@mba:bin$ls /Library/Caches/Homebrew/
- Formula/ wget-1.14.tar.gz
- coreutils-8.22.tar.xz wget-1.15.tar.gz
- openssl-1.0.1f.mavericks.bottle.tar.gz xz-5.0.5.mavericks.bottle.2.tar.gz
- gerryyang@mba:bin$ls /Library/Caches/Homebrew/Formula/
- coreutils.brewing openssl.brewing wget.brewing xz.brewing
- gerryyang@mba:bin$
(5) 我可以安裝自己的stuff在/usr/local嗎?
Yes, brew is designed to not get in your way so you can use it how youlike.
Install your own stuff, but be aware that if you install commonlibraries, like libexpat yourself, it may cause trouble when trying tobuild certain Homebrew formula. As a resultbrew doctor
will warn youabout this.
Thus it’s probably better to install your own stuff to the Cellar andthen brew link
it. Like so:
$ cd foo-0.1 $ brew diy ./configure —prefix=/usr/local/Cellar/foo/0.1 $ ./configure —prefix=/usr/local/Cellar/foo/0.1 [snip] $ make && make install $ brew link foo Linking /usr/local/Cellar/foo/0.1… 17 symlinks created
在我的環境上檢測是這樣的:
- gerryyang@mba:bin$brew doctor
- Warning: Unbrewed dylibs were found in /usr/local/lib.
- If you didn't put them there on purpose they could cause problems when
- building Homebrew formulae, and may need to be deleted.
- Unexpected dylibs:
- /usr/local/lib/libboost_atomic.dylib
- /usr/local/lib/libboost_chrono.dylib
- /usr/local/lib/libboost_context.dylib
- /usr/local/lib/libboost_date_time.dylib
- /usr/local/lib/libboost_filesystem.dylib
- /usr/local/lib/libboost_graph.dylib
- /usr/local/lib/libboost_iostreams.dylib
- /usr/local/lib/libboost_locale.dylib
- /usr/local/lib/libboost_log.dylib
- /usr/local/lib/libboost_math_c99.dylib
- /usr/local/lib/libboost_math_c99f.dylib
- /usr/local/lib/libboost_math_c99l.dylib
- /usr/local/lib/libboost_math_tr1.dylib
- /usr/local/lib/libboost_math_tr1f.dylib
- /usr/local/lib/libboost_math_tr1l.dylib
- /usr/local/lib/libboost_prg_exec_monitor.dylib
- /usr/local/lib/libboost_program_options.dylib
- /usr/local/lib/libboost_python.dylib
- /usr/local/lib/libboost_random.dylib
- /usr/local/lib/libboost_regex.dylib
- /usr/local/lib/libboost_serialization.dylib
- /usr/local/lib/libboost_signals.dylib
- /usr/local/lib/libboost_system.dylib
- /usr/local/lib/libboost_thread.dylib
- /usr/local/lib/libboost_timer.dylib
- /usr/local/lib/libboost_unit_test_framework.dylib
- /usr/local/lib/libboost_wave.dylib
- /usr/local/lib/libboost_wserialization.dylib
- Warning: Unbrewed static libraries were found in /usr/local/lib.
- If you didn't put them there on purpose they could cause problems when
- building Homebrew formulae, and may need to be deleted.
- Unexpected static libraries:
- /usr/local/lib/libboost_atomic.a
- /usr/local/lib/libboost_chrono.a
- /usr/local/lib/libboost_context.a
- /usr/local/lib/libboost_coroutine.a
- /usr/local/lib/libboost_date_time.a
- /usr/local/lib/libboost_exception.a
- /usr/local/lib/libboost_filesystem.a
- /usr/local/lib/libboost_graph.a
- /usr/local/lib/libboost_iostreams.a
- /usr/local/lib/libboost_locale.a
- /usr/local/lib/libboost_log.a
- /usr/local/lib/libboost_math_c99.a
- /usr/local/lib/libboost_math_c99f.a
- /usr/local/lib/libboost_math_c99l.a
- /usr/local/lib/libboost_math_tr1.a
- /usr/local/lib/libboost_math_tr1f.a
- /usr/local/lib/libboost_math_tr1l.a
- /usr/local/lib/libboost_prg_exec_monitor.a
- /usr/local/lib/libboost_program_options.a
- /usr/local/lib/libboost_python.a
- /usr/local/lib/libboost_random.a
- /usr/local/lib/libboost_regex.a
- /usr/local/lib/libboost_serialization.a
- /usr/local/lib/libboost_signals.a
- /usr/local/lib/libboost_system.a
- /usr/local/lib/libboost_test_exec_monitor.a
- /usr/local/lib/libboost_thread.a
- /usr/local/lib/libboost_timer.a
- /usr/local/lib/libboost_unit_test_framework.a
- /usr/local/lib/libboost_wave.a
- /usr/local/lib/libboost_wserialization.a
- Warning: You have unlinked kegs in your Cellar
- Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
- those kegs to fail to run properly once built. Run `brew link` on these:
- wget
- gerryyang@mba:bin$
(6) 為什麼選擇Homebrew這個名字?
mxcl was too concerned with the beer theme and didn’t consider that the project may actually prove popular. By the time he realized it was too late. However, today, the first google hit for “homebrew” is not
beer related ;-)
看來作者是個酒鬼!
(7) 關於更多地QA可以參考Homebrew的Wiki[4]
(8) 一些常用安裝推薦
- gerryyang@mba:bin$brew list
- coreutils openssl wget xz
Introduction to Coreutils
The GNU Core Utilities are the basic file, shell and text manipulationutilities of the GNU operating system.
These are the core utilitieswhich are expected to exist on every operating system.
相關文章
- Mac安裝HomebrewMac
- mac下安裝homebrewMac
- MAC: Homebrew(代替yum)安裝Mac
- Mac電腦安裝homebrewMac
- M1 Mac安裝 HomebrewMac
- Mac 下使用homebrew安裝NVMMac
- Homebrew安裝
- Mac下通過Homebrew安裝MySQLMacMySql
- Mac安裝homebrew證書過期Mac
- Homebrew 安裝 openssl
- Mac-Homebrew 安裝/解除安裝/更換國內映象源Mac
- Mac上HomeBrew安裝及換源教程Mac
- 安裝HomeBrew提示已安裝並無法解除安裝
- Mac下安裝node-opencvMacOpenCV
- caffe安裝系列——安裝OpenCVOpenCV
- M1 Mac 12.1 Homebrew安裝GoMacGo
- MAC下的homebrew安裝及映象調整Mac
- M1安裝homebrew
- homebrew下載安裝及mysql的下載安裝MySql
- Homebrew 換源安裝與使用
- Mac下獲取Homebrew安裝的軟體路徑Mac
- python安裝opencvPythonOpenCV
- Mac OS下用Homebrew安裝自己寫的開源工具Mac開源工具
- HomeBrew 安裝 yarn 1.6.0 版本失敗Yarn
- ffmpeg安裝之mac安裝Mac
- 【轉】Mac端包管理工具——Homebrew簡介及安裝Mac
- mac系統使用homebrew 無法安裝nodejs解決方案MacNodeJS
- python如何安裝OpenCV?PythonOpenCV
- windows安裝cmake、opencv、qtWindowsOpenCVQT
- Linux下安裝OpenCVLinuxOpenCV
- Mac Redis安裝與解除安裝MacRedis
- Mac Mojava 透過 homebrew 安裝 PHP5.6 版本正確姿勢MacJavaPHP
- Mac Mojava 通過 homebrew 安裝 PHP5.6 版本正確姿勢MacJavaPHP
- 記錄Mac Pro M1晶片安裝HomeBrew的過程吧Mac晶片
- 樹莓派opencv的安裝樹莓派OpenCV
- Ubuntu 14.04 下安裝 OpenCVUbuntuOpenCV
- opencv4.5 帶cuda 安裝OpenCV
- Mac 安裝 NginxMacNginx