Compile and Use Boost Libraries in Xcode & Visual Studio

A quick note on Boost. If you want to use the Boost libraries, the good news is that you can just #include the boost classes that you want to use in your code (see bottom for Boost usage in code).
However if you want to use libs such as Boost::Filesystem or Boost::iostreams, they MUST be compiled into a library file. This is accomplished with the bjam tool which is kinda like an easy to use compiler for Boost instead of using GCC.
I can't believe that there are no tutorials on the net for compiling Boost on Mac OS X. I got it compiled for Visual Studio 2005 after reading numerous articles and then I adapted it for Mac OS X. Actually it's pretty easy to compile on Mac, a little easier than it was on Windows...OK, here we go!
Download Bjam and the Boost source from http://www.boost.org/
Compiling for Xcode 3.1:
1. You need to download and copy bjam.exe to
/usr/sbin
This will make the command available in the Terminal.
If it does not, check your paths in ~/.profile using vi or TextEdit. Find the line that starts with export PATH= and copy bjam to one of the paths listed (note: each path is separated by a colon ":")
2. CD to the boost directory (for example):
cd ~/Desktop/Boost/boost_1_37_0
3. To compile for Visual Studio 2005 as static release libs:
bjam toolset=darwin variant=release threading=multi link=static
4. The static debug version:
bjam toolset= darwin variant=debug threading=multi link=static
NOTE: If you want dynamic libraries (.dylib files instead of .a files) then use"link=shared instead of"link=static.
This will take a while (maybe 20 minutes) to run each of the above commands, and you"ll need both.
A Note about the naming of the output files:
Building with link=static gives you files with the following naming convention:
libboost_filesystem-xgcc40-mt-d-1_37.a
Building with link=shared gives you files with the following naming convention:
libboost_filesystem-xgcc40-mt-1_37.a
Including Boost in your C++ code
#include<iostream>
#include<boost/any.hpp> // any is an actual library
C++ Usage
boost::any a(5);
a = 7.67;
std::cout << boost::any_cast <double> (a) << std::endl;
Using the Compiled Libraries in Xcode
Using the libraries can be accomplished by dragging the static (.a) lib files to tyhe Xcode project. If you really want to use dynamic library files, then you will need to drag to the Xcode project, and also have a script run after the binary is built.
This is because dynamic libraries need to be copied inside the .app bundle because they are linked to at runtime, as opposed to compile time for static libs.
The actual process of scripting the dylib copy and modifying the darwin executable are already covered in another article I wrote here: How To use Dynamic Libraries in Xcode 3.1
Using the Header files in Xcode
Okay, it's getting to be all a big pain in the ass now, but we're almost done.
The headers can usually be dragged to the Xcode project for individual .h files. However because the Boost headers are in nested folders, it's a lot easier to just add the path to the Xcode project.
Go to: Project | Edit Project Settings | Build tab | User Header Search Paths
Add the path to where Boost is located on your hard disk. A good place is to put it somewhere inside the /Developer folder or /usr/local/include/. Also DO NOT check the "Recursive" option. It won't work if you do.
It's up to you where you want to put it....
Compiling for Visual Studio 2005/2008:
1. You need to download and copy bjam.exe to
C:Windows\system32
2. CD to the boost directory:
cd C:Program FilesBoostboost_1_37_0
3. To compile for Visual Studio 2005 as static release lib (use"msvc-8.0 for VS 2008):
bjam toolset=msvc-8.0 variant=release threading=multi link=static
The static debug version:
bjam toolset=msvc-8.0 variant=debug threading=multi link=static
Here is a dynamic dll, debug runtime version (for Visual Studio 2008). It will produce files with the following file name convention:" 'libboost_math_c99-vc90-mt-sgd-1_37.lib'
bjam toolset=msvc-9.0 variant=debug threading=multi runtime-link=static
This will take a while (maybe 20 minutes) to run each of the above commands, and you"ll need both.
A Note about the naming of the output files:
Building with link=static gives you files with the following naming convention:
boost_filesystem-vc80-mt-1_37.lib
Building with link=shared gives you files with the following naming convention:
libboost_filesystem-vc80-mt-1_37.dll
Update: Other options for compiling Boost with Bjam
Within the final build of the libraries, you will get a lib file like the following:
'libboost_math_c99-vc90-mt-sgd-1_37.lib'
The "lib" prefix is for static libraries. Use link=static
The 's' letter is to static linking to runtime. Use runtime-link=static
The 'd' is debug, use variant=debug
The 'g' is using debug runtime, to enable this you will need: runtime-debugging=on.
For more information, you can find the official documentation for building Boost here:
http://www.boost.org/doc/libs/1_38_0/more/getting_started/index.html
Well, thats it. Thanks for reading and if you have anything to add please leave a comment below (Sorry about the registration, I get too much spam otherwise).
Cheers,
Brock
