Getting started with the Boost libraries in Ubuntu Linux

1. Install the Boost libraries from the command line

First try the following

$ sudo apt-get install libboost*

You may get an error message similar to the following, like I did:

E: Unable to correct problems, you have held broken packages.

Upon reading the online forums, I then tried the next commands:

$ sudo apt-get clean && sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install libboost*

But no luck. I was still getting the same error message.

This is what actually worked for me:

$ sudo apt-get install libboost-all-dev

2. Create a a sample project utilizing Boost

$ mkdir boost
$ cd boost/
$ cat > main.cpp &

Using the Linux-based text editor of your choice (I often use gedit or emacs), paste the following code sample into main.cpp that makes use of the Boost formatting capabilities. This is the same code as was used in the setting up of Boost for Visual Studio example:

#include <iostream>
#include <boost/format.hpp>

using namespace std;
using namespace boost;

int main()
{
    unsigned int arr[5] = { 0x05, 0x04, 0xAA, 0x0F, 0x0D };

    cout << format("%02X-%02X-%02X-%02X-%02X")
            % arr[0]
            % arr[1]
            % arr[2]
            % arr[3]
            % arr[4]
         << endl;
}

3. Compile and run the program

$ g++ -o main *.cpp
$ ./main

Giving the following formatted hexadecimal output:

05-04-AA-0F-0D

Another example: getting started with Boost threads

Another code sample, this time demonstrating the use of Boost:threads in
a Linux environment:

#include <boost/thread/thread.hpp>
#include <iostream>

void hello()
{
    std::cout <<
    "Hello world, I'm a thread!"
    << std::endl;
}

int main(int argc, char* argv[])
{
    boost::thread thrd(&hello);
    thrd.join();
    return 0;
}

And be sure to incude -lboost_system -lboost_thread in your compilation:

$ g++ -o main *.cpp -lboost_system -lboost_thread
$ ./main

Giving the following output:

Hello world, I'm a thread!

`