Posts

Handy archive of boost binaries for Windows / Visual Studio

Every now and again I have to work on an old software project that may use an older version of the boost libraries. Well if like me, you occasionally find that you are missing some boost .lib file or other binary then have a look at this very handy archive of the boost windows binaries maintained by Thomas Kent:

http://boost.teeks99.com/

Thanks Thomas, very handy!

boost_software_library

C++ thread safe blocking queue using boost

In Software Engineering some patterns reoccur only every few years. The blocking queue pattern is on such for me. Here is a great article on an implementation of a thread-safe blocking queue that uses boost for synchronisation:

http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

I implemented something like this some years ago (without the benefit of boost etc.) and now I need one again and I want to make it bang-up-to-date wrt. C++11 and boost etc., so this article is a real bonus, much thanks to Anthony Williams!

Write jpeg with header comment – boost generic image library (gil)

I had to hack the living daylights out of something today, and although I am not proud of it, needs must where the devil drives!

I was using the boost generic image library (gil) for managing some camera images and using jpeg_write_view() for saving them as JPEGs. A requirement emerged to insert a comment into the header of the saved images, but the gil does not support this out of the box.

The gil uses libjpeg for writing its JPEGs and adding a comment in libjped is as easy as calling jpeg_write_marker(). The problem was that the gil buries libjpeg calls under a few classes so I had to copy and paste some of this in order to redefine a new writer which supports comments.

Anyway I have included the extra code inline below, it adds a new helper function called jpeg_write_view_comment()

Use this new function like this:

Here is the extra code which I placed in a file called jpeg_writer_comment.h

Boost link error – undefined reference to boost filesystem detail copy_file( )

I had some difficulty today getting my code to link to boost::filesystem::copy_file() (linking to boost version 1.53.0, gcc v4.7.2), I suffered from link errors like this:

undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)’

From reading this post, it seemed that my problem may have stemmed from my use of c++0x.

 

None of the suggested solutions in this thread worked for me & I double checked that had built boost with -std=c++0x.

 

In the end after looking through the code in filesystem/operations.hpp the only thing that fixed my linking problem was to #define BOOST_NO_CXX11_SCOPED_ENUMS before including , like this:

This may well not be the best/proper way to fix the problem but it was the only thing that worked for me..

 

Other relevant links that I found are here, here and here.

 

boost filesystem compile warnings – boost system posix_category defined but not used

If you are using boost::filesystem in your C++ code and getting are some build warnings along the lines of:

 

‘boost::system::posix_category’ defined but not used

 

Then you may find that inserting the following code before #including boost/filesystem gets rid of them for you:

This gets rid of some the deprecated stuff that was causing the warnings.

 

I found this handy tip here:

 

http://stackoverflow.com/questions/1814548/boostsystem-category-defined-but-not-used

 

Eclipse CDT – Using boost thread library with eclipse

If you are scratching your head about how to setup Eclipse and CDT to use the boost thread library then have a look at the first post in this thread:

 

http://www.eclipse.org/forums/index.php/m/787571/

 

It explains which project settings need to be changed.

 

Thanks to Robert!

 

Raspberry Pi – Install boost 1.50 C++ Libraries on Wheezy

If you are doing modern C++ development these days you will be using the boost libraries. I have started using the Raspberry Pi as a stand-in ARM platform for some smart-cam development while I wait for the actual hardware to become available and as such need to install boost. To this end, the boost 1.50 libs can be installed on wheezy as follows:

Now for a little example of using and building with the libs, consider this little test program that uses the boost regex library:

If we stick this code into a file called it.cpp then we can build it using C++0x (C++11 ish), the standard c++ lib and the boost regex lib like this:

 

This will output to a.out, which can then be executed like this:

 

boost C++ read from serial port with timeout example

If you are doing any serial port communications these days in C++ and would like your code to be portable, then you are probably using boost’s asio::serial_port class.

 

One complication with using serial_port (and boost::asio more generally) is that it doesn’t provide a direct facility to allow synchronous blocking reads to time-out and return if no data arrives within a specified time period. Here is a little example that tries to read a character from COM3 (on windows..)

 

 

In this example read() will block forever if no data arrives to the serial port, this is not always what you want, especially when dealing with possibly noisy or unreliable rs232 communication.

 

In order to take advantage of read time-outs you have to issue asynchronous reads and incorporate a deadline_timer which will cancel the read after a specified time, i.e. if the read hasn’t received the data it was expecting before the deadline_timer expires, then it will be cancelled.

 

Using asynchronous IO in boost is a bit involved and it can be quite quite messy, so I have written small class called blocking_reader which will block while trying to read a single character, and will time out if a character hasn’t been received in a specified number of milliseconds. It can be used like this:

 

The above code isn’t the most sensible or efficient but it shows the use of blocking_reader, which in this case times out reads after 500ms.

 

You open the serial_port as normal and then pass it to blocking_reader’s constructor along with a timeout value. You then use blocking_reader.read_char() to read a single character. If the read times out then read_char() will return false (otherwise it will return true!)

 

The code for blocking_reader can be downloaded from here, I have also included it below: