Installing C++ Libraries on OS X

青春壹個敷衍的年華 提交于 2019-11-26 18:36:34

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!