Goto to official Boost website. And download the latest version of boost available.
Open the tar ball and extract to any folder. In my case it's Downloads
folder. Once you extract boost it will create a folder like so Downloads/boost_1_67_0
.
Note: Your folder may vary depending on the version of boost you downloaded.
Since I downloadedboost_1_67_0
my extracted folder is named accordingly.
./bootstrap.sh --with-python=/usr/bin/python3.6 --prefix=<where/to/install>
./b2 --layout=versioned variant=debug link=static threading=multi install
cd ./boost_1_67_0
./bootstrap.sh --prefix=/home/preet/devenv/boost_1_67_0 # --prefix=/path/to/install/boost
./bootstrap.sh
takes bunch of arguments to view all the arguments just type
./bootstrap.sh --help
. --prefix
option is used to specify that we want to install boost in specified directory and not to system directory (And installing it by explicitely specifying --prefix is the RECOMMANDED way!)
If you execute just ./bootstrap.sh
then it will be configured to install into system directory (Which again is not recommanded, but it's your choice.)
After you have finished executing project-config.jam
file will be generated in to current working directory (i.e., Downloads/boost_1_67_0/project-config.jam
in my case.)
Here you can see contents of my project-config.jam
file.
# Boost.Build Configuration
# Automatically generated by bootstrap.sh
import option ;
import feature ;
# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
using gcc ;
}
project : default-build <toolset>gcc ;
# Python configuration
import python ;
if ! [ python.configured ]
{
using python : 2.7 : /usr ;
}
# List of --with-<library> and --without-<library>
# options. If left empty, all libraries will be built.
# Options specified on the command line completely
# override this variable.
libraries = ;
# These settings are equivivalent to corresponding command-line
# options.
option.set prefix : /home/preet/devenv/boost_1_67_0 ;
option.set exec-prefix : /home/preet/devenv/boost_1_67_0 ;
option.set libdir : /home/preet/devenv/boost_1_67_0/lib ;
option.set includedir : /home/preet/devenv/boost_1_67_0/include ;
# Stop on first error
option.set keep-going : false ;
After bootstraping you will find that two more executable are generated in current directory b2
and bjam
. Both the executable are essentially the same executable with different name
Downloads
└── boost_1_67_0
├── b2
├── bin.v2
├── bjam
├── boost
├── boost-build.jam
├── boostcpp.jam
├── boost.css
├── boost.png
├── bootstrap.bat
├── bootstrap.log
├── bootstrap.sh
├── doc
├── index.htm
├── index.html
├── INSTALL
├── Jamroot
├── libs
├── LICENSE_1_0.txt
├── more
├── project-config.jam
├── rst.css
├── status
└── tools
./b2 install --prefix=/home/preet/devenv/boost_1_67_0
After executing above command you will see that you have include
and lib
folder created in your prefix
path.
Note:
./b2
can take any of the two optioninstall
andstage
. And./b2
is same as./b2 stage
. For more information type./b2 --help
.
Also note that by default both static and dynamic libraries are build if you want only specific library you can specify that. For more information type ./b2 --help
Most of the Boost source is header only so, there is no need to link them externally just provide proper path to header file and you are good to go.
Certain files requires both compiling and linking. If you choose to link static library then its very easy, but for dynamic library you need few extra steps.
// test.cpp
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
g++ -I /home/preet/Downloads/boost_1_67_0/boost/ -o test test.cpp
echo 1 2 3 | ./test
//test.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
g++ -I /home/preet/Downloads/boost_1_67_0/ -o main main.cpp -L /home/preet/devenv/boost_1_67_0/lib/ -lboost_regex
Note: If you invoke above command and you have both static and dynamic library then compiler will link any of the library. If compiler links static library then your program will run fine. But if compiler links dynamic library then may be your program will not run and it will throw runtime error.
Because your dynamic linker is unable to link dynamic library at runtime. We will see how to fix this.
So, for linking static library execute following command.
g++ -I /home/preet/Downloads/boost_1_67_0/ -o main main.cpp /home/preet/devenv/boost_1_67_0/lib/libboost_regex.a
g++ -I /home/preet/Downloads/boost_1_67_0/ -o main main.cpp /home/preet/devenv/boost_1_67_0/lib/libboost_regex.so
This will compile your program but if your try to run it, it will throw an error, because during compilation we said we are going to use dynamic library so compiler was happy, but during runtime when program is requesting for dynamic library, dynamic linker is not able to locate our dynamic library (here libboost_regex.so
).
And it makes sense because we only compiled Boost from source, but we never told dynamic linker that you must scan prefix/lib
path as well because we have our boost's dynamic libraries build there.
So, let's inform dynamic linker about our dynamic libraries that we built.
cd /etc/ld.so.conf.d/
sudo echo "/home/preet/devenv/boost_1_67_0/lib" > boost.conf
sudo ldconfig -v # -v => verbose
Close and reopen the terminal. And now if you try to run your program it will run fine.
To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
Example shown here are taken directly from Boost official website.
For more details check out this link.
Dynamic libraries and ldconfig