Installing C++ Libraries on OS X
Solution 1:
Before you can do any C/C++ development work on a Mac, you need to go to the App Store and download Xcode
for free - it is Apple's IDE - Integrated Development Environment. Without Xcode
, you will have no compiler (i.e. clang
or gcc
or g++
) and no build tools, (i.e. make
).
Install Xcode
If you are totally new to Mac, App Store
looks like this:
and Xcode
looks like this:
Install Command Line Tools
Next you must install Xcode's command-line tools, so start a Terminal - by pressing ⌘+SPACE and starting to type Terminal
and when it guesses correctly, just hit Enter/Return. Copy and paste the following into Terminal and hit Enter/Return.
xcode-select --install
The above is called a "Spotlight Search" and is the easiest way to find anything on a Mac.
Install homebrew
Then, if you want to install OpenCV
on a Mac, install a package manager such as homebrew
which is a matter of copying and pasting a single line from the homebrew website into your Terminal. I will not show the line here in case it ever changes and someone looks at this in a few years, but it is easy to see if you go to the link above.
Find Packages
Then you can find any packages you want with:
brew search opencv # Look for packages called "opencv"
or
brew search boost # Look for "boost" libraries
Install OpenCV
So, for a vanilla (no special options) installation and build of OpenCV
do this:
brew install opencv
Remove Packages
You can later remove any packages you no longer want with:
brew rm opencv
Update Packages
You can also update all installed packages with:
brew update && brew upgrade && brew cleanup
Build a Project
Once you have got it installed, you can start compiling and building your own project. It helps if you use the pkg-config
package to pick up all the necessary compiler/linker settings you need, so I would suggest:
brew install pkg-config
Now you can compile and link with a really simple command like:
g++ $(pkg-config --cflags --libs opencv) process.cpp -o process
Then you can go on to use Xcode
IDE later if you want to once you get started.
Build with Xcode
Once you have got started with basic compilation, you may want to start using Xcode
to edit your programs, to do that, you must tell Xcode
where the header files are and also where the libraries are and which libraries to link. This will vary with your OpenCV version, but you will need to alter the places marked in the two diagrams below. You will find these easily if you click them in order - green area first, then the yellow, then the blue, then the red.
The actual information that will need to go in the Xcode settings areas I have marked above can be found by running the same pkg-config
command I suggested in the previous section. So run:
pkg-config --cflags opencv
to get the location of the header (include) files, and then run
pkg-config --libs opencv
to get the information you need to fill in for the linker in Xcode
.