Configure Boost (C++ Libraries) on Xcode, Code::Blocks and Visual Studio

Boost is a set of libraries for the C++ programming language that provide support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains over eighty individual libraries.[2]

The other interesting points of Boost are :

  1. Open source
  2. Cross platform
  3. Complement to STL rather than a replacement
  4. Many of Boost developers are on the C++ standard committee
  5. Well documented
  6. Most of the Boost libraries are licensed under the Boost Software License, designed to allow Boost to be used with both free and proprietary software projects

Installation Boost

Before jumping into steps of configuring Boost on various IDE, let’s begin with Boost installation. To be noted that on this post I run Xcode on OS X, Code::Blocks on Linux (Ubuntu) and Visual Studio on Windows. The detail environments I use are :

  1. OS X 10.11.4 El Capitan
  2. Ubuntu 14.04.4 LTS
  3. Xcode Version 7.2
  4. Code::Blocks 13.12, gcc 4.8.4
  5. Visual Studio 2013
  6. Boost 1.60.0

OS X and Linux (Ubuntu)

There are several ways of Boost installation. Instead of build from source code, we can use package manager such as MacPorts, Homebrew, Advance Package Tool, etc. In this post we will build Boost from source code. The installation steps (from source code) on OS X and Ubuntu are the similar. To make it consistent, I use the same installation path for OS X and Ubuntu that is /usr/local/boost_1_60_0. You can use different path if you want. The steps are :

  1. Download boost library from Boost website
  2. Extract it.
  3. Open terminal, navigate to the extracted directory
  4. Create directory on /usr/local/boost_1_60_0, and ensure IDE has access to the directory. On my case I don't need this step on OS X, but on ubuntu it does. ``` bash sudo mkdir /usr/local/boost_1_60_0 sudo chmod 777 -r boost_1_60_0 ```
  5. Run command : ``` bash ./bootstrap.sh --prefix=/usr/local/boost_1_60_0 ./b2 install ``` This last step quite take time. So you can have coffee while waiting for it :)

Once the installation finish, we should have generated directory. They are /usr/local/boost_1_60_0/include contains header files and /usr/local/boost_1_60_0/lib contains libraries.

Windows

The Boost installation step on Windows is also similar to the installation step on OS X and Ubuntu. The steps are :

  1. Download boost library from Boost website
  2. Extract it to C:\\boost_1_60_0
  3. Open Visual Studio command prompt. I use Visual Studio 2013 x86 Native Tools Command Prompt native tool (I have not test using default Windows Command Prompt) ``` bat C:\> cd C:\boost_1_60_0 C:\boost_1_60_0> bootstrap.bat C:\boost_1_60_0> .\b2 ``` As on OS X and Ubuntu, the last step quite take time.

Configure Boost on IDE(s)

Before create C++ projects on various IDE, let’s create a simple C++ hello world code that use Boost libraries. To simplify the test, I grab sample code from here

#include <iostream>
#include <fstream>
using namespace std;

#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
using namespace boost;
using namespace boost::iostreams;

int main(int argc, const char * argv[]) {

    typedef tee_device<ostream, ofstream> TeeDevice;
    typedef stream<TeeDevice> TeeStream;

    ofstream ofs("/Users/neutro/Workspace/cpp/sample.txt");

    TeeDevice my_tee(cout, ofs);
    TeeStream my_split(my_tee);

    my_split << "Hello, World!\n";

    my_split.flush();
    my_split.close();

    return 0;
}

The snipped code above just print text and write it to a text file. We just want to ensure the IDE’s compiler can compile and build the code that includes Boost libraries.

  • Xcode

    To include Boost libraries on Xcode project :
    1. Select Xcode project > Build Setting
    2. Add /usr/local/boost_1_60_0/include/ to the Header Search Paths
    3. Add /usr/local/boost_1_60_0/lib/ to the Library Search Paths
  • Code::Blocks

    To include Boost libraries on Code::Blocks project :
    1. Right Click on Code::Blocks project > Build Option
    2. Select Compiler tab, add /usr/local/boost_1_60_0/include/
    3. Select Linker tab, add /usr/local/boost_1_60_0/lib/
  • Visual Studio

    To include Boost libraries on Visual C++ project :
    1. Right Click on VC++ project > Properties
    2. Select VC++ Directories on the left pane
    3. Add C:\boost_1_60_0 on Include Directories item
    4. Add C:\boost_1_60_0\stage\lib on Include Directories item
    5. Click OK to close the dialog

The last is rebuild the above code on selected IDE. We should not got any errors once the IDE can detect the Boost directory path.

The sample of this article can be downloaded here

Reference

  1. http://www.boost.org/
  2. Wikipedia