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 :
- Open source
- Cross platform
- Complement to STL rather than a replacement
- Many of Boost developers are on the C++ standard committee
- Well documented
- 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 :
- OS X 10.11.4 El Capitan
- Ubuntu 14.04.4 LTS
- Xcode Version 7.2
- Code::Blocks 13.12, gcc 4.8.4
- Visual Studio 2013
- 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 :
- Download boost library from Boost website
- Extract it.
- Open terminal, navigate to the extracted directory
- 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 ``` - 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 :
- Download boost library from Boost website
- Extract it to C:\\boost_1_60_0
- 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 :- Select Xcode project > Build Setting
- Add
/usr/local/boost_1_60_0/include/
to the Header Search Paths - Add
/usr/local/boost_1_60_0/lib/
to the Library Search Paths
-
Code::Blocks
To include Boost libraries on Code::Blocks project :- Right Click on Code::Blocks project > Build Option
-
Select Compiler tab, add
/usr/local/boost_1_60_0/include/
-
Select Linker tab, add
/usr/local/boost_1_60_0/lib/
-
Visual Studio
To include Boost libraries on Visual C++ project :- Right Click on VC++ project > Properties
- Select VC++ Directories on the left pane
- Add
C:\boost_1_60_0
on Include Directories item - Add
C:\boost_1_60_0\stage\lib
on Include Directories item - 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
-
http://www.boost.org/
-
Wikipedia
- http://www.boost.org/
- Wikipedia