Help Installing C++ Library in El Capitan (boost)

Discussion in 'Mac Programming' started by Aquamite, Jan 22, 2016.

  1. Aquamite macrumors regular


    Oct 2, 2014
    I have no experience with using other libraries in C++ other than the STL. I have been using clang++ for compiling small C++ codes and projects using Terminal commands with makefiles and rarely I've had to use XCode. However it had to come the day I had to move beyond that soft code beach and step into the big jungle of option crowded IDE's and C++ libraries.

    I've downloaded from here: the latest version of the boost library. I've unziped it in a folder. Then I've opened a Terminal in the unziped folder called "boost" and naively without having read the installation instructions deeply thinking that it would be easy to get it to work I've executed "./" and then "./b2" and after a (veeeeery long) while it seems like it finished the installation.

    And after that, when I come to compile the code where I use some .hpp from the boost library with clang++ using the terminal I get the following error:

    fatal error: 'boost/asio.hpp' file not found
    #include <boost/asio.hpp>
    I don't know if that error means that the installation of boost hasn't been successful or what. I can't even find a way to confirm that the installation of the library performed well. I've googled a lot trying to find a solution and some say to try to compile the code using XCode changing some header parameters that I don't know about. I do really get lost inside big IDEs sea of options and like to keep things clear.

    Can anybody help me with this? It's the first time I'm using external libraries with C++.

    Thanks a lot.
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Post the exact command line that invokes clang++.

    Where did the installer install the header files? You'll need to know that.

    At the command-line, you must add directories where includes are looked for. Look at the man-page for clang++ and read about the options for how to specify include-file dirs.

    The option is -Idir, but read the man-page for details.

    You'll probably need a similar command-line option to tell clang where to find the library for linking. You'll need to know that, too.
  3. teagls macrumors regular

    May 16, 2013
    First of all you should install boost using homebrew to make sure it is done correctly. Then you have to add the search path for the boost headers from something like /usr/local , and link the library in Xcode. To put it in simple terms. Xcode has no idea you have it installed where it is at and where the actually compiled boost library is. You have to point it to the library and the header files so you can make API calls to the library.
  4. Aquamite thread starter macrumors regular


    Oct 2, 2014
    Me neither... :(

    clang++ -o *.cpp
    I don't know that I've searched for "boost" with finder in /usr and it didn't find anything.

    The man-page only shows this:
    -idirafter <value>      Add directory to AFTER include search path
    And also:
               Add the specified directory to the search path for include files.
    I'd rather do it without third party software that I don't fully understand.
  5. teagls macrumors regular

    May 16, 2013
    Homebrew is a package manager for mac. "It's all git and ruby underneath". All it does is download the appropriate version from git for your system. Compiles and builds it and then puts it into a standard location and creates a symlink so it can be easily accessed from other applications/tools like Xcode. To say you'd rather do it without third party software is naive. You should really read more in detail what it is and what a package manger is. It would probably also help you understand how to include external libraries and headers in Xcode.
  6. r03dz macrumors member

    Aug 5, 2014
    Homebrew is glorious. It's open source, all pull requests are reviewed before they are accepted. I use it everyday. brew install boost
    --- Post Merged, Jan 22, 2016 ---
    I am just curious here. What particular feature are you going to be using from Boost?
  7. Aquamite thread starter macrumors regular


    Oct 2, 2014
  8. aiusepsi, Jan 26, 2016
    Last edited: Jan 26, 2016

    aiusepsi macrumors newbie

    Jun 5, 2014
    I'm going to add to the chorus saying to use Homebrew. It's totally great, pretty much every library you could ever want to install is on there, it makes your life much, much easier, and pretty much everyone uses it. And while it is third-party, it's a) open source, b) the original creator now works on the Swift dev tools at Apple. So there's that.

    As far as using Boost goes, you need to tell the compiler where to find the header files. Boost.Asio is header-only, but other Boost libraries aren't header only, and in those cases you could also need to tell the linker where to find the compiled libraries and which libraries to link. It may be that Asio requires Boost.System to be linked; I'd have to check.

    Adding an extra include path is easy: when you invoke clang++, add the option -I/usr/local/include as /usr/local/include is where headers installed by Homebrew go. If you installed Boost some other way you'll have to figure out where the Boost build scripts put this stuff by default. This is one of the good things about Homebrew, you learn once that everything you need is in /usr/local, and then everything just works.

    To link libraries, you need to give the linker (which in this case, is also clang++) the option -L/usr/local/lib (path will differ if not using Homebrew) and then use -l to bring in any libraries you want, for example -lboost_system to link
    libboost_system.a (the compiler adds the "lib" and the extension for you).
    --- Post Merged, Jan 26, 2016 ---
    This compiles for me fine with Homebrew-installed Boost:
     // boost_test.cpp
    #include <boost/asio.hpp>
    int main()
    clang++ -I/usr/local/include -L/usr/local/lib -lboost_system boost_test.cpp

Share This Page