Solarian Programmer

My programming ramblings

Compiling Boost with GCC or Clang on macOS

Posted on August 7, 2018 by Paul

In this article I will show you how to build the Boost libraries under macOS with GCC 8 or Clang. Once the libraries are installed, we’ll test the build with a short demo of using Boost Filesystem.

First, you will need to download the latest stable version of Boost, I will use version 1.68.0. Extract the archive and open a Terminal in the Boost folder.

If you prefer to use Clang, the build is straightforward, just use the next instructions:

1 ./bootstrap.sh --prefix=/usr/local/boost-1.68.0
2 sudo ./b2 cxxflags=-std=c++17 install

The above will set the installation path to /usr/local/boost-1.68.0 and start the installation process.

You will also need to add the Boost libraries to the dynamic libraries path with:

1 export DYLD_LIBRARY_PATH=/usr/local/boost-1.68.0/lib:$DYLD_LIBRARY_PATH

you can save the above export line to your .bash_profile file for changing the path permanently or use the above line only when you need it, the path will revert to the default value once you close your Terminal.

For GCC, the process is a bit more complicated, first be sure that you have GCC 8 installed and available in your path. You can build the latest stable version of GCC, currently 8.1.0, if you follow my previous tutorial.

Start by bootstrapping the installer:

1 ./bootstrap.sh --prefix=/usr/local/boost-1.68.0

Once the above is finished, open project-config.jam in a text editor and comment these lines:

1 #if ! darwin in [ feature.values <toolset> ]
2 #{
3 #    using darwin ;
4 #}
5 
6 #project : default-build <toolset>darwin ;

Now, we can specify the version of GCC we want to use by adding an extra line to project-config.jam:

1 #project : default-build <toolset>darwin ;
2 
3 using gcc : 8.1 : /usr/local/gcc-8.1/bin/g++-8.1 ;

Feel free to change 8.1 to match the version of GCC you have on your Mac, save and close project-config.jam.

At this point, you are ready to build and install Boost:

1 sudo ./b2 cxxflags=-std=c++17 install

You will also need to add the Boost libraries to the dynamic libraries path with:

1 export DYLD_LIBRARY_PATH=/usr/local/boost-1.68.0/lib:$DYLD_LIBRARY_PATH

you can save the above export line to your .bash_profile file for changing the path permanently or use the above line only when you need it, the path will revert to the default value once you close your Terminal.

Let’s test our freshly built Boost with a C++ code that uses filesystem to print the current directory and his content, save the next code example in a file named test.cpp:

 1 #include <iostream>
 2 #include <boost/filesystem.hpp>
 3 
 4 int main()
 5 {
 6 	// Get the current directory
 7 	auto path = boost::filesystem::current_path();
 8 	std::cout << path << "\n";
 9 
10 	// Print the content of the current directory
11 	for(auto &entry : boost::filesystem::directory_iterator(path))
12 	{
13 		std::cout << entry << std::endl;
14 	}
15 
16 	return 0;
17 }

Please note, that a C++ library compiled with GCC is not compatible with Clang and vice-versa. So, you won’t be able to use a Clang build Boost with GCC …

For the GCC version, you can compile and run the above code with:

1 g++-8.1 -std=c++17 -I /usr/local/boost-1.68.0/include -L /usr/local/boost-1.68.0/lib test.cpp -o test -lboost_system -lboost_filesystem
2 ./test

For the Clang version, you can compile and run the above code with:

1 clang++ -std=c++17 -I /usr/local/boost-1.68.0/include -L /usr/local/boost-1.68.0/lib test.cpp -o test -lboost_system -lboost_filesystem
2 ./test

If you want to learn more about Boost I would recommend reading The Boost C++ Libraries by B Schaling:

or Learning Boost C++ Libraries by A. Mukherjee:


Show Comments