How to install latest Boost library on Raspberry Pi

How to install latest boost library on Raspberry Pi

How to install latest Boost library on Raspberry Pi

What you will need

  • CMake
  • Boost Library

CMake Installation

You can refer to this link to install latest CMake for your Raspberry Pi.

Boost Library Installation

Preparation

Official boost library installation for Unix variants can be found from here. You can also check the latest version of Boost library from here.

Note: At the time of writing, the latest version of Boost library is 1.60

Download and Extract file

$ mkdir boost
$ cd boost
$ wget http://sourceforge.net/projects/boost/files/boost/1.60.0/boost_1_60_0.tar.bz2
$ tar xvfo boost_1_60_0.tar.bz2
Install Full Boost Library

$ cd boost_1_60_0
$ ./bootstrap.sh
$ sudo ./b2 install 

Note: You can also install the library that you need. It saves time.

Install necessary Boost Libraries

It takes a lot time to build complete Boost library even in Linux PC. If you know what library you are going to use, then you can limit the number of libraries to be built. For my case, I am going to use Boost.Asio. After I look at this page to check what kinds of libraries I need, I found these Boost.System,Boost.Regex,OpenSSL,Boost.Thread, Boost.Date_Time, Boost.Serialization libraries are required.
Therefore, I use the following command to install.

$ ./bootstrap.sh --with-libraries=date_time,filesystem,iostreams,math,regex,serialization,signals,system,thread,timer
$ sudo ./b2 install 

Note: you can use ./bootstrap.sh --show-libraries to see all available libraries.

Note: Even I only build with the limited number of libraries, it took more than 20mins on Raspberry Pi 2.

Testing the Boost Library

We are going to test Boost library whether it was installed correctly or not. Since I am going to use Boost.Asio later on my project, I will use asio header file to test.

$ mkdir BoostTest
$ cd BoostTest
$ gedit main.cpp

Copy the following code and save it as main.cpp.

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

int main( int argc, char * argv[] )
{
    boost::asio::io_service io_service;
    for( int x = 0; x < 42; ++x )
    {
        io_service.poll();
        std::cout << "Counter: " << x << std::endl;
    }
    return 0;
}

And of course, we are going to use CMake.

$ gedit CMakeLists.txt

Copy the following text to CMakeLists.txt and save it.

cmake_minimum_required(VERSION 3.3)
project(BoostTest)

FIND_PACKAGE( Boost 1.60 COMPONENTS system REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(BoostTest ${SOURCE_FILES})
target_link_libraries(BoostTest ${Boost_LIBRARIES} -lpthread)
Compile C++ file

$ cmake .
$ make

Once the compilation is successfully, you can use the following command to run the program.

$ ./BoostTest

It will just print out 42 lines. Now I know more or less that the libraries I installed are okay.

Note: I tried to compilie on both Ubuntu and Raspberry pi. -lpthread is necessary to put it in the CMake file for Raspberry Pi. But it is not necessary for Ubuntu tho.

Useful Links

I found some of these links while I am troubleshooting.


1 comment: