Introduction
Boost is a collection of high-quality, peer-reviewed C++ libraries that extend the functionality of the C++ standard library. It’s widely used in professional software development for tasks ranging from multi-threading and regular expressions to data structures and mathematical operations. This tutorial will guide you through installing Boost on Ubuntu, along with a basic example of how to use one of its libraries.
Installation Methods
There are two primary methods for installing Boost on Ubuntu: using the package manager (apt
) or building from source.
Method 1: Using apt
(Package Manager)
The simplest way to install Boost is through Ubuntu’s package manager. Open your terminal and run the following command:
sudo apt update
sudo apt install libboost-all-dev
This command installs the development files for all Boost libraries, allowing you to compile and link your programs against them.
Using a PPA (Personal Package Archive)
Sometimes, the version of Boost available in the standard Ubuntu repositories might be outdated. If you need a more recent version, you can use a PPA. Run these commands:
sudo add-apt-repository ppa:boost-latest/ppa
sudo apt update
sudo apt install libboost-all-dev
This adds the Boost Latest PPA to your system, updates the package list, and then installs the development files.
Method 2: Building from Source
If you require a specific version of Boost or want more control over the build process, you can build it from source. Here’s how:
-
Download the Source: Download the desired Boost version from the official Boost website (https://www.boost.org/users/history/). For example, to download version 1.55:
wget -O boost_1_55_0.tar.gz https://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.gz/download
-
Extract the Archive:
tar xzvf boost_1_55_0.tar.gz
-
Navigate to the Boost Directory:
cd boost_1_55_0
-
Install Dependencies: Boost relies on several dependencies. Install them using:
sudo apt-get update sudo apt-get install build-essential g++ python3-dev autotools-dev libicu-dev libbz2-dev
-
Bootstrap and Build: Run the bootstrap script, then build Boost.
./bootstrap.sh --prefix=/usr/local ./b2
To utilize multiple cores for faster building, determine the number of cores available on your system and use the
-j
flag. For example, if you have 8 cores:./b2 -j 8
-
Install Boost: Finally, install Boost with:
sudo ./b2 install
This will install the Boost libraries and headers to
/usr/local/lib
and/usr/local/include
, respectively. If/usr/local/lib
is not in your library path, you may need to add it:sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/local.conf' sudo ldconfig
Using Boost: A Simple Example
Let’s demonstrate how to use Boost with a basic example using the boost::array
library.
-
Create a C++ File: Create a file named
main.cpp
with the following content:#include <iostream> #include <boost/array.hpp> using namespace std; int main() { boost::array<int, 4> arr = {{1, 2, 3, 4}}; cout << "hi" << arr[0] << endl; return 0; }
-
Compile the Code: Compile the code using
g++
:g++ -o s main.cpp
-
Run the Executable:
./s
This will print "hi1" to the console, demonstrating that you have successfully installed and used Boost.
Conclusion
This tutorial has provided a comprehensive guide to installing and using Boost on Ubuntu. Whether you choose the package manager or the build-from-source method, you should now be able to leverage the powerful capabilities of Boost in your C++ projects.