Solarian Programmer

My programming ramblings

Compiling Boost with GCC 5 or Clang on Mac OS X

Posted on March 6, 2016 by Sol

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

The next C++ standard, C++17, will probably include a much needed Filesystem specification based on Boost Filesystem. Some compilers like Visual Studio 2015 already include the filesystem header as an experimental feature. In the meantime, if your preferred C++ compiler doesn’t include filesystem, you can use the Boost implementation and update your code later.

First, you will need to download the latest stable version of Boost, I will use version 1.60.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.60.0
2 sudo ./b2 cxxflags=-std=c++14 install

The above will set the installation path to /usr/local/boost-1.60.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.60.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 5 installed and available in your path. You can build the latest stable version of GCC, currently 5.3.0, if you follow my previous tutorial.

Start by bootstrapping the installer:

1 ./bootstrap.sh --prefix=/usr/local/boost-1.60.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 : 5.2.0 : g++-5.2.0 ;

Feel free to change 5.2.0 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++14 install

When you run the above command you will see some link related errors, this is because Apple’s linker, ld, doesn’t accept all options that GNU ld accepts. You can ignore these errors, the build process will create only static versions of the Boost libraries.

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 << std::endl;
 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++-5.2.0 -std=c++14 -I /usr/local/boost-1.60.0/include -L /usr/local/boost-1.60.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++14 -I /usr/local/boost-1.60.0/include -L /usr/local/boost-1.60.0/lib test.cpp -o test -lboost_system -lboost_filesystem
2 ./test

don’t forget to set the dynamic libraries path if you want to be able to run the executable created by Clang.

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:

comments powered by Disqus