Libraries
arrow_drop_down
News
Learn
Community
Libraries
Releases
news
learn
community
libraries
releases
User Guide
Contributor Guide
Formal Reviews
Last Updated :
16 Oct, 2022
Boost library is a set of a popular collection of peer-reviewed, free, open-source C++ libraries. It supports a number of tasks such as unit testing, image processing, multithreading, and mathematical aspects such as linear algebra and regular expressions. You can also store, numbers that are out of range of long long, or double. It was first made available on September 1st, 1999. There are 164 different libraries in it. In this article, we will learn, how to install the boost library in C++ on Windows.
Installing Boost Library in C++ on Windows:
Step 1: Go to Boost.org. Click on the Downloads option on the right side.
Step 2: Click on the boost_1_72_0.zip file, to download the required boost library. It has an approx. size of 200MB.
Step 3: Now, open the location where your zip file is downloaded. For example: This PC > Local Disk (C:) > Users > jh > Downloads > .
Step 4: Select the zip file. Right-Click on it, and select Extract All…
Step 5: The files get extracted at the same location, with the same folder name. Now, go to Program Files, and create a new folder name Boost.
Step 6: Now, copy the extracted folder boost_1_72_0 into the boost folder. Hence, the boost library is installed into our system.
Verify the Installation of the Boost Library in C++
The successful compilation of the code will prove that the boost library is installed in windows.
C++
#include <boost/array.hpp> #include <iostream> using namespace std; int main() { boost::array<int, 10> arr = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } }; for (int i = 0; i < 10; i++) { cout << "Geek Rank is :" << arr[i] << "*" << "\n"; } return 0; }
Output:
Geek Rank is :1* Geek Rank is :2* Geek Rank is :3* Geek Rank is :4* Geek Rank is :5* Geek Rank is :6* Geek Rank is :7* Geek Rank is :8* Geek Rank is :9* Geek Rank is :10*
Introduction
Boost is easy when you are using headers or pre-compiled binaries for visual studio, but it can be a pain to compile from source on windows, especially when you want the 64-bit version of MinGW to use gcc/g++. This installation process should be thorough enough to simply copy and paste commands, but robust enough to install everything you need.
Note: if you need to install any of the libraries that need dependencies, see this great answer from stack overflow
Get files needed for install
Get the MinGW installer mingw-w64-install.exe from Sourceforge
Get the boost_1_68_0.zip source from Sourceforge
Note: This should work perfectly with other versions of boost as well
Copy these to a new folder
C:\install
It should now contain the following two files
mingw-w64-install.exe
boost_1_68_0.zip
Install MinGW-w64
Run the installer
Run mingw-w64-install.exe
Click next
Change the Architecture from i868
to x86_64
Click next and keep the default install location
Click next to start the install
Click Finish to exit the installer
After the install, add a hard link (junction) to the folder
Open a command prompt AS ADMIN
- windows key -> type «cmd»
- right click «command prompt»
- Run as administrator
Enter the following command to create a link to MinGW folder in C:\
mklink /J C:\MinGW "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64"
Add MinGW to the system PATH
Add this to the session and system PATH environment variable
set PATH=%PATH%;C:\MinGW\bin
setx /M PATH "%PATH%"
Check to ensure proper install
g++ --version
should return the following info
Install boost
Navigate to install
cd C:\install
unzip to «install/boost_1_68_0»
powershell -command "Expand-Archive C:\install\boost_1_68_0.zip C:\install"
This takes about 15 minutes
cd C:\install\boost_1_68_0
Make directories for building and install
mkdir C:\boost-build
mkdir C:\install\boost_1_68_0\boost-build
mkdir C:\boost
Boost.Build setup
cd C:\install\boost_1_68_0\tools\build
prepare b2
bootstrap.bat gcc
Build boost.build with b2
b2 --prefix="C:\boost-build" install
Add C:\boost-build\bin to your session PATH variable
set PATH=%PATH%;C:\boost-build\bin
Building Boost
navigate back up to the boost unzipped root directory
cd C:\install\boost_1_68_0
Build boost with b2
b2 --build-dir="C:\install\boost_1_68_0\build" --build-type=complete --prefix="C:\boost" toolset=gcc install
This is going to take awhile, so try to run this command right before beginning the director’s cut of Lord of the Ring Return of the King.
When this is done you should see the following output
You can now delete «C:\install» and «C:\boost-build»
Adding to projects
Everything should now be installed
Include folder:
C:\boost\include\boost-1_68
Linker folder:
C:\boost\lib
Link required libraries:
e.g. libboost_atomic-mgw81-mt-d-x64-1_68.a
This package contains the Boost C++ Libraries development files.
Install Boost (Ubuntu)#
Usually, you’ll want to install all available Boost libraries. Note that you will be prompted for your password upon using sudo
.
sudo apt install libboost-all-dev
In case you just want specific components of the Boost framework, check available packages (libboost-<component>-dev
) first. For Boost.Thread:
sudo apt install libboost-thread-dev
Header-only Boost libraries are grouped in one single package, which is a common dependency for libboost-all-dev
and libboost-<component>-dev
:
sudo apt install libboost-dev
Install Boost (Windows)#
- Download the source code from http://www.boost.org/users/download/.
- Extract in a
Boost
folder located atC:\
orC:\Program files
so that CMake find-modules can detect it. - Invoke the command line and navigate to the extracted folder (e.g.
cd C:\Boost\boost_1_63_0
). - Follow the instructions at http://www.boost.org/build/doc/html/bbv2/overview/invocation.html to build all packages in the selected configuration. You may want to compile only specific modules, in which case refer to their documentation (http://www.boost.org/doc/libs/).
- Run the bootstrap script (
bootstrap.bat
) prior to building anything. - Issuing
b2
will build only static libraries for the default architecture and both Release and Debug configurations. - Issue
b2 variant=release address-model=64 link=static,shared
to build 64-bit static and dynamic binaries in Release configuration for all available Boost components. - Issue
b2 property=value --with-<component>
to build only specific Boost components (list). Use lower case. Example:b2 variant=release --with-thread
will build release binaries for Boost.Thread. You can chain multiple--with-<component>
options. - See #Properties for further details on the command line interface.
- Run the bootstrap script (
The Boost libraries come as source code. While most of the libraries consist solely of header files that can be used directly, some of the libraries require compilation. In order to make installation as easy as possible, an automated installation process based on Boost.Build is available. Instead of validating and compiling individual libraries separately, Boost.Build installs the complete set automatically. Boost.Build can be used with many operating systems and compilers and is able to compile each individual library based on appropriate configuration files.
To automatically install the Boost libraries with Boost.Build, the command-line program bjam is used. The Boost libraries ship this program as source code and not as an executable. Therefore, two steps are required to build and install the Boost libraries. After you download the Boost libraries, change to the Boost directory and enter the following commands on the command line:
-
Enter
bootstrap
on Windows and./bootstrap.sh
on other platforms, such as Linux, to compile bjam. The script automatically searches for a C compiler to build bjam. -
Then, enter
bjam
on Windows and./bjam
on other platforms to start installing the Boost libraries.
You use bootstrap only once to build bjam. However, you might need to use bjam more often because bjam supports command-line options to build the Boost libraries in different ways. If you start bjam without any command-line options, a default configuration will be used. Because the default configuration is not always appropriate, you should know the most important command-line options:
-
The command-line options
stage
andinstall
specify whether the Boost libraries are installed in a subdirectory calledstage
or are made available system wide. The meaning of system wide depends on the operating system. On Windows, the target directory isC:\Boost
; on Linux it is/usr/local
. The target directory can also be specified with the--prefix
option. Starting bjam without command-line options always meansstage
. -
If bjam is called without any command-line options, it will search for a suitable C++ compiler. A specific compiler can be selected using the
--toolset
option. To select Visual C++ 2013 on Windows, call bjam with--toolset=msvc-12.0
. To select the GCC compiler on Linux, use--toolset=gcc
. -
The command-line option
--build-type
determines which build types of the libraries are created. By default, this option is set tominimal
, meaning that only release builds are created. This may become an issue for developers who want to create debug builds of their projects with Visual C++ or GCC. Because these compilers automatically try to link against the debug builds of the Boost libraries, an error message will be displayed. In this case the option--build-type
should be set tocomplete
to generate both debug and release builds of the Boost libraries. This can take quite some time, which is whycomplete
is not the default. -
Boost libraries that have to be compiled are made available on Windows with file names that contain version numbers and various tokens. They make it possible, for example, to tell whether a library has been built as a debug or release variant.
libboost_atomic-vc120-mt-gd-1_56
is such a file name. This library was built with Visual C++ 2013. It belongs to the Boost libraries 1.56.0. It is a debug variant and can be used in multithreaded programs. With the command-line option--layout
, bjam can be told to generate other file names. For example, if you set it tosystem
, the same file would be calledlibboost_atomic
. On Linux,system
is the default setting. If you want file names on Linux to be the same as those generated on Windows by default, set--layout
toversioned
.
To create both debug and release builds of the Boost libraries with Visual C++ 2013 and install them in the directory D:\Boost
, enter the following command:
bjam —toolset=msvc-12.0 —build-type=complete —prefix=D:\Boost install
To build them on Linux and install them in the default directory, the command would be:
bjam —toolset=gcc —build-type=complete install
There are many other command-line options that you can use to specify in detail how to compile the Boost libraries. Have a look at the following command:
bjam —toolset=msvc-12.0 debug release link=static runtime-link=shared install
The debug
and release
options cause both debug and release builds to be generated. link=static
only creates static libraries. runtime-link=shared
specifies that the C++ runtime library is dynamically linked, which is the default setting for projects in Visual C++ 2013.