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:
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:
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:
Once the above is finished, open project-config.jam in a text editor and comment these lines:
Now, we can specify the version of GCC we want to use by adding an extra line to project-config.jam:
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:
You will also need to add the Boost libraries to the dynamic libraries path with:
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:
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:
For the Clang version, you can compile and run the above code with:
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: