Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing C++ Libraries on OS X

I am trying to get my head around some basic concepts, but I can't seem to figure them out.

I am really confused over what it means to install (I think they are called libraries) for C++. I am trying to install OpenCV, but I don't know what needs to happen for it to be installed, how to check, or what really OpenCV is (Is it a library, framework, something else?).

My understanding is that OpenCV (and other libraries/frameworks) is distributed as only the source code so that is is able to work cross-platform. Then, after you download it, you have to build it (I don't know what build means though), and then link your compiler against it so that it can access the files? I don't know how any of this would be done, or really what this means. I think a lot of this is done by CMake, but I don't know what CMake really does, how you would use it, or how you would then utilize the library/framework in your code. Also, where would the libraries like OpenCV be installed, how would clang (or any other compiler/linker) know where to find them, and what kind of files would they be (.a, .dylib, .cpp, .hpp, executables, or a collection of everything)? Is this structure specific to C++ and OS X or is it more widespread?

I am not looking for a tutorial on how to install OpenCV or other libraries, but I am instead trying to learn how that actually works so I won't need tutorials in the future.

like image 502
Giesbrecht Avatar asked Dec 17 '15 17:12

Giesbrecht


People also ask

How do I download C library on Mac?

You can install the libraries from Xcode, under the option "install command line tools". If you specifically want gcc instead of clang, you can install it using macports or homebrew.

Where does OSX install libraries?

New libraries are typically installed under /usr/local/include, but it's going to depend on how you built and installed the library. Once you find the header files, try adding -I/path/to/headers in your compile command.

Where is C++ library in Mac?

/usr/include contains the system and c libraries. The c++ libc++ library headers are part of XCode and are therefore inside its application bundle. ... and that's what most people use these days (i.e. the libraries shipped with Xcode).


1 Answers

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:

enter image description here

and Xcode looks like this:

enter image description here

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.

enter image description here

enter image description here

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.

like image 91
Mark Setchell Avatar answered Sep 20 '22 17:09

Mark Setchell