Compile the static Boost Libraries in Windows

From The DarkMod Wiki
Revision as of 22:11, 7 April 2012 by Serpentinee (talk | contribs) (Added an alternative CMake build method)
Jump to navigationJump to search

There are several methods to obtain Boost binaries for Windows build environments.

The boost static libraries follow a naming convention as explained in the boost online docs: http://www.boost.org/doc/libs/1_45_0/more/getting_started/windows.html#library-naming When including boost headers in your MSVC++ projects, the headers (like <boost/regex.hpp>) make use of VC's auto linking feature, so you don't need to worry about the actual file names, the linker is automatically instructed to require the correct libraries. All you have to do is make sure the linker can find these files in your project's libs directory.

Download them from sourceforge

You can try to find the static binaries needed on boost's sourceforge page: http://sourceforge.net/projects/boost/files/boost-binaries/

Often for very "fresh" releases there are no binaries available yet, so you'll need to fall back compiling the binaries yourself.

Compile the libraries from Boost sources

Download the boost release package from their website, e.g. Boost 1.45, and extract it to a folder. For this guide, I'll assume your boost package has been saved to c:\tools\boost_1_45_0).

To build the static libraries Boost you need the the boost jam tool (bjam.exe). At the time of this writing there is a precompiled bjam.exe availabe on sourceforge. Save the bjam.exe into some place you remember (e.g. c:\tools\bjam.exe). In case the bjam.exe can not be found, you can also build it from sources (this is not within the scope of this article).

Let's start compiling by using the boost.filesystem library as example. Open a command console (Win-R ==> "cmd" => Enter).

cd c:\tools\boost_1_45_0
cd libs
cd filesystem
cd build
c:\tools\bjam toolset=msvc link=static threading=multi release stage
c:\tools\bjam toolset=msvc link=static threading=multi debug stage

Note that TDM itself needs an additional switch runtime-link=static in addition to the above:

c:\tools\bjam toolset=msvc link=static threading=multi runtime-link=static release stage
c:\tools\bjam toolset=msvc link=static threading=multi runtime-link=static debug stage

After these commands are done, the libboost_filesystem_vc*.lib files (both debug and release variants) can be found in the stage/ directory below your boost folder. You'll need to repeat the above steps and replace filesystem with the names of the other libraries, e.g. regex, thread or system.

  • Note for x64 users: Add the address-model=64 option to bjam (after the threading argument) in order to build static libs with the 64-bit compiler.
  • Note for users with multiple versions of Visual Studio: you can specify exactly which toolset you want to use by setting toolset=msvc-9.0 (for VC++ 2008) and toolset=msvc-10.0 (for VC++ 2010).

Now take the *.lib files from the stage/ folder and copy them into your project's library folder and the linking should succeed.

Boost.Python

To compile the Boost.Python static lib, you'll need to have a Python distribution installed in your system, otherwise bjam will complain that no Python installation can be found. Download a package from the Python website and install it.

Note: the boost python static library (e.g. libboost_python-vc100-mt-gd-1_42.lib) will contain a reference to a specific version of the python static library. If you downloaded Python 2.7.1, your boost static lib will contain a reference to python27.lib. This file can be found in the libs/ in the directory you installed Python to, you should add that file to your project's library directory as well.

Another note: when linking against that python27.lib your application will require the python27.dll at runtime. This file can be somewhat tricky to locate. For 32 bit systems this is usually C:\Windows\system32, for x64 systems the 32 bit Python DLL can be found in your C:\Windows\SysWoW64\ folder. On x64 systems, the 64 bit Python DLL is right in C:\Windows\System32.

Boost.CMake

Some people find bjam to be extremely problematic under Windows. If you are having trouble, I would suggest trying the CMake build system branch of Boost. This is also very helpful with compiling the libs for use with Intel's compilers which tend to be badly supported by the bjam build system. Another advantage is that CMake generates Visual Studio solution files, meaning that you can adjust build settings or rebuild components with a familiar interface.

CMake in Windows has a nice GUI which makes configuring the build very easy, as well as providing a way to easily adjust them at a later stage.

Boost.Cmake - Boost Wiki - Boost.Cmake - Gitorious

Steps

CMake:

  1. Download the latest binary release of CMake for your platform CMake and related downloads
  2. Install it. Make sure that the CMake GUI gets installed.

Boost.CMake Source:

  1. Figure out which release of Boost that you need — in this case we will assume 1.45.
  2. Download the relevant release tarball from the Gitorious branch (or use git/hg-git). The tarball can be found in the branch overview i.e, : Boost.CMake 1.45 branch
  3. Unpack the tarball somewhere.

Configuring and Generating:

  1. Run the CMake GUI tool.
  2. Set the source path to the location where you unpacked the tarball.
  3. Set an (empty) build directory - Note: while it says that this is where the binaries will be placed, this is actually going to be where all of the temporary build files are located, it will also contain the libs if we use default paths - don't freak out if there's loads of other stuff.
  4. Click 'Configure' and set set which toolset/compiler you will be using. Note: If you are using icc, you will want to use Visual Studio as the generator but specify the compilers manually (icl.exe).
  5. There should now be a grid of default build options, many in red. Adjust them if needed, make sure that ENABLE_STATIC, ENABLE_MULTI_THREADED and ENABLE_RELEASE are ticked.
  6. Once the configure process is complete, click 'Generate' - This will now place the solution file into the build directory.

Building:

  1. Go to the build directory, as specified above.
  2. Open the solution file.
  3. Note: By default the libraries are built as multithreaded-dll's (/MD flag) this needs to be changed to normal multithreaded for use with TDM. If you are having trouble with TDM linking looking in some strange libs/dlls, this is why.
    1. Select all of the projects that you wish to build, then right click on them in the solution browser and go to 'Properties'.
    2. Expand the C++ tab => Code Generation => Runtime Library, change this to 'Multi-threaded (/MT).
    3. Apply.
  4. While they are still selected, right click on them and then build them.

You should now have the .libs in a /lib folder within your working directory. The libs might need renaming to fit with the naming scheme that is expected, e.g, : boost-regex-intel-mt-1_45.lib => libboost-regex-iw-mt-s-1_45.lib. This could likely be fixed in the CMakeLists file if you wanted, as well as the /MD -> /MT switch.