Using boost library on Windows

From this stackoverflow question:
MSVC has very poor support for the C language, they do not support anything past C90. Herb Sutter has already publicly stated this in his blog.
cstdint is supported from MSVC2012.
There is a msinttypes project that fills the absence of stdint.h and inttypes.h in Microsoft Visual Studio.
Boost also provides boost/cstdint.hpp if you do not have it.

Boost library can be very useful but it is also very big so in this post I’m going to show how to compile it from source code and then how to extract one of its modules and include it in your projects source code.

I also have another post where I compiled Boost.

Go to the Boost site and download boost source code, I got the 1.59 version of it (boost_1_59_0.zip). Unzip it to your local folder, I used the C:\Users\pedro\dev-VS2008\boost_1_59_0 folder. Then open up a Command Prompt (you don’t need a Visual Studio 2008 Command Prompt) and do:

cd C:\Users\pedro\dev-VS2008\boost_1_59_0
bootstrap.bat

after bootstraping in done, you need to run .\b2 to compile. You can ask for a particular tool set and build type. If you don’t give any parameters to .\b2 then it will built the msvc12 tool set in dynamic link build type. If you want to build all types for msvc9 tool set then it will be like this:

.\b2 –toolset=msvc-9.0 –build-type=complete

Otherwise just use it without parameters for msvc12 tool set dynamic link:

.\b2

after a lot of message lines later it should say that the libraries were successfully built:

The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
C:\Users\pedro\dev-VS2008\boost_1_59_0
The following directory should be added to linker library paths:
C:\Users\pedro\dev-VS2008\boost_1_59_0\stage\lib

Ok, we have our Boost library ready to use! If you’re just going to extract a piece of it into your own project you don’t need this, but if you’re going to use Boost for command line compiled projects it is useful to have a couple of environment variables pointing to Boost, so lets do it, open the Windows Advanced System Settings and add the new environment variables:

windows-advanced-system-settings

Open the Environment Variables and add the BOOST_ROOT environment variable to the system variables:

add-boost-root

Then add the BOOST_LIBRARYDIR environment variable to the system variables:

add-boost-librarydir

Again, the environment variables are only needed if you are going to use Boost in a command line compile, like for example the openCONFIGURATOR tool, check out this other post.

In order to copy only a part of the Boost libraries into your own project, you need the Boost bcp command and you need to build it explicitly:

.\b2 tools/bcp

Don’t confuse Boost bcp with MS bsp! (bulk copy program for MS SQL Server)

Now that we have the boost bcp tool we can use it. I have a project where I need a tokenizer and I know that Boost has one, so lets extract this only from Boost and place it in my projects folder tree:

C:\Users\pedro\dev-VS2008\boost_1_59_0>dist\bin\bcp boost\tokenizer.hpp C:\Users\pedro\dev-VS2008\MyProject\boost_1_59_0

This will create a new folder named boost inside the boost_1_59_0 folder and it will copy only the tokenizer related files.

then go to project properties and add the library base folder $(SolutionDir)\boost_1_59_0 to the project includes:

proj-settings-include-boostand in your .cpp file add the corresponding include:

#include <boost/tokenizer.hpp>

Now, I encountered a strange issue reading a string that contained the degree symbol ASCII 176 (0xb0), an assert inside the tokenizer code, something like: Debug Assertion Failed (unsigned)(c+1) <= 256. I found lots of people complaining about this issue but no solution…

Tracing inside the tokenizer code I found out that we don’t call the asserting code if BOOST_NO_CWCTYPE is defined, so I added this to my .cpp and it works:

#define BOOST_NO_CWCTYPE

And there you go, a great tokenizer to enjoy in your project!

Advertisements

Author: pedro_nf

Independent contractor on IT solutions and automation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s