Click here to Skip to main content
Click here to Skip to main content

Building Boost libraries for Visual Studio

By , 18 Jul 2007
 

Introduction

The Boost initiative is the next hottest thing after STL for the C++ community. While the documentation is quite good, it can be overwhelming to get a quick start. This simple article explains a method of building Boost libraries. I wrote it because many questions in the newsgroups are about linker errors, which happen if you try to link against some of the Boost libraries. The original article was written with Boost version 1.33, but the updated version works only with the new Bjam syntax which was introduced with version 1.34.

Boost

While most of the Boost libraries are template-based, some of them - e.g. date-time, regex, filesystem, signals - come with CPP source files and thus require actual code to be generated. Boost uses a build system called Bjam, which can be configured for a lot of platforms. I work only on Windows with Visual Studio 2003, I'll describe here a configuration for this combination. At work though, we already use Visual Studio 2008.

Building Boost

After downloading the Boost libraries and unpacking them in a folder - referenced in this article with <boost_root>, e.g. on my PC it is C:\work sdk\boost, the libraries need to be built:

  1. Build Bjam
  2. Build the Boost libraries

Building Bjam

Bjam can be built in two ways:

  • Use the 'bootstrap.bat' batch file located in <boost_root>. Run this batch file and Bjam will be automatically built and added to the root.
  • Build Bjam 'manually': browse to <boost_root>\tools\build\jam_src and run build.bat from a command prompt. This will start to build Bjam, which ends up in <boost_root>\tools\build\jam_src\bin.ntx86\bjam.exe. Copy this file to the root of Boost, <boost_root>.

Building Boost Libraries

The Bjam system must be told that you want to build debug/release libraries, build with threading in mind, and link against various options of the STL or platforms. I only use Visual Studio with Plauger's STL, but use two different versions of Visual Studio:

  1. Build a debug build with multithreading and dynamic linking for VC++ 7.1.
  2. Build a release build with multithreading and dynamic linking for VC++ 7.1.
  3. Build a debug build with multithreading and dynamic linking for VC++ 9.0.
  4. Build a release build with multithreading and dynamic linking for VC++ 9.0.

With the release of Visual Studio 2005 / 2008, Microsoft supports side by side installations of the crt. With manifests, one can bind a module to a specific version of the crt. The Boost libraries are dependent on the crt as well, and you can instruct the build process to use the latest version through a '_BIND_TO_CURRENT_VCLIBS_VERSION' define. This leads to the following options for Visual Studio 2003 / 2008:

  1. bjam toolset=msvc-7.1 variant=debug threading=multi link=shared
  2. bjam toolset=msvc-7.1 variant=release threading=multi link=shared
  3. bjam toolset=msvc-9.0 variant=debug threading=multi link=shared define=_BIND_TO_CURRENT_VCLIBS_VERSION
  4. bjam toolset=msvc-9.0 variant=release threading=multi link=shared define=_BIND_TO_CURRENT_VCLIBS_VERSION

I've already made four batch files with the same command(s):

Copy these batch files to the root of Boost, <boost_root>. Now the libraries can be built:

  1. Open the command prompt.
  2. Run vcvars32.bat from Visual Studio.
  3. Run the batch files, e.g. zbuilddebug_vc71.bat.

Bjam will now try to build the libraries.

Building All

For the stressful software engineer, I offer here two batch files which will do it all together: they build bjam if it does not exist and build the debug and release mode libraries in one go:

After the build process has finished, the library executables are created in the <boost_root>\bin.v2 directory, e.g.:

  • boost_date_time-vc71-mt-gd-1_41.dll for the multithreaded debug build of a date-time library.
  • boost_date_time-vc71-mt-1_41.dll for the multithreaded release build of a date-time library.

Because all of these files are built under their own library names, one can copy them all - i.e. search on 'mt-gd-1_41 and mt-1_41 - to one location and adjust Visual Studio to point to this library location. Alternatively one can use the <boost_root>\stage directory. However this directory does not contain the associated PDBs, which is unhandy during debugging.

Using Boost Libraries

We have built the shared libraries and therefore we have to instruct Visual Studio to link against them. Fortunately, Boost has incorporated an "autolink" feature. Thus, with the preprocessor one can link to the correct libraries. Define the following preprocessor statements:

  • BOOST_ALL_DYN_LINK
  • BOOST_LIB_DIAGNOSTIC

Even better, include a header file in your precompiled header before including a Boost header:

Room for Improvement

  • For some reason, the "normal," i.e. non-dynamic, libraries get built.
  • Python library should also get built, but for some reason it gets excluded.
  • Visual Studio 2005 introduces the safe C and iterator concepts. Boost libraries don't like them and should be turned off.

History

  • 11 September, 2005 - Original version posted
  • 18 July, 2007 - Updated with Boost 1.34 syntax
  • 25 November 2009 - Updated with manifests and the 'all' batch file; removed old syntax

Credits

  • Leon Wiertz: tip about manifests and the 'all' batch file

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gast128
Software Developer (Senior)
Netherlands Netherlands
Born in the Netherlands in 1971. Spent since 1998 years as a software engineer programming Windows application(s) using, UML, C++, MFC, STL, DirectShow and Boost.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionboost library configurationmemberranjithkumar8116-Jan-13 1:47 
Hi there,
 
I am new using boost library, pls help me to configure the boost in visual studio 2010.
 
please paste some screen shot for configuration , then its helpful for me.
 
Regards,
Ranjith
AnswerRe: boost library configurationmemberGast12827-Mar-13 11:44 
Hello,
 
Visual Studio 2010 supports the concept of 'property pages' in which you can specify library directories. The MSDN link is http://msdn.microsoft.com/en-us/library/669zx6zc%28v=vs.100%29.aspx, but the explanation is not very good.
 
Alternative you can use the 'traditional' way and just put your library directories in the project properties. This is not advised, but I show it here anyway because it's easier to explain. Note that my boost root directory is "c:\work sdk\boost_1_53_0" and I copied the build lib and dll files to "c:\work\bin\debug". Unfortunately I see no option here to paste images, so i have to explain it by text.
 
Add to your vcxproj file:
- Configuration Properties, C/C++, General, Additional Include Directories: "%(AdditionalIncludeDirectories);C:\Work Sdk\boost_1_53_0" (without quotes).
- Configuration Properties, Linker, General, Additional Library Directories: "\work\bin\debug" (without quotes).
 
Note that you still have to build Boost and use the auto link feature to get everything working.
QuestionBlueGomemberVertexwahn24-Oct-12 8:31 
I build a small tool (called BlueGo) which builds boost libraries using Visual Studio 2010/12 x64 automatically. You just have to start the application, select your configuration and hit the Build button - everything else works automatically. The application downloads the library, extracts it and builds it. It can be downloaded from my website: http://vertexwahn.de/[^]
GeneralMy vote of 5memberLegor21-Aug-12 23:21 
Thanks really helpfull!
QuestionBeautiful - Thanksmemberpkbmx17-Jul-12 18:48 
Only one thing, both release and debug links for the .bat files give you the debug file. Not too tough to fix though compared to three hours surfing ...
GeneralThanksmemberrabipatra_k18-Jun-12 11:27 
helped me a lot
GeneralMy vote of 5memberJesusATG29-Apr-12 21:51 
Perfect explanation about build and configure boost libraries into Visual Studio Enviroment.
Questionfatal error LNK1104: cannot open file 'libboost_regex-vc80-mt-gd-1_42.lib'memberAdeel Mirza21-Jun-11 0:42 
I am using 1_42 version of Boost Lib and I have to use the 64-bit version. For that I used this command.
 
bjam toolset=msvc-8.0 variant=debug threading=multi link=shared define=_BIND_TO_CURRENT_VCLIBS_VERSION address-model=64
 
It all went well. I included the Inc and lib file created in my project. But when I compiled my project it showed me this error
 
fatal error LNK1104: cannot open file 'libboost_regex-vc80-mt-gd-1_42.lib'
 
When I searched for this library in boost folder it didn't exist. Looks like it wase'nt created while compiling using BJam. can someone help me in resolving this problem?
 
Thanks in Advance
Maverick

AnswerRe: fatal error LNK1104: cannot open file 'libboost_regex-vc80-mt-gd-1_42.lib' [modified]memberGast12821-Jun-11 6:59 
Your project looks for the static library versions (libboost*.lib), instead of the DLL versions. Use the auto linking feature of Boost for correct linking, e.g. use '#define BOOST_ALL_DYN_LINK' before any boost header.

modified 30-Apr-12 4:08am.

GeneralNaming conventionmemberAdeel Mirza21-Feb-11 2:18 
I have an existing application . I downloaded boost library "boost_1_35_0" from this website
 
http://sourceforge.net/projects/boos.../boost/1.35.0/
 
and extracted it in program folder
 
C:\Program Files\boost\boost_1_35_0
 
and tried your steps, ran those batch files. Since I am using visual studio 2005 so have to make changes in the batch file. I replaced VC9.0 by VC8.0. After running those batch files instead of "libboost_regex-vc80-mt-1_35.lib" it only creates "boost_regex-vc80-mt-gd-1_35.lib". My program always looks for "libboost_regex-vc80-mt-1_35.lib" and throws error
Maverick

GeneralRe: Naming conventionmemberGast12821-Feb-11 7:53 
The libboost*.* variants are the static libraries. Either uses dynamic linking (either by Boost's auto linking feature '#define BOOST_ALL_DYN_LINK' or use #include "BoostDefines.hpp" from article before any boost header) or build the static libraries (through runtime-link=static)
GeneralBoost for 64-Bit Windows 7memberz-big9-Jul-10 8:00 
How do I build boost libs for Windows 7 64-bit. I have Visual Studio 9. Any help is appreciated. Thank you.
GeneralRe: Boost for 64-Bit Windows 7memberGast12812-Jul-10 7:12 
add 'address-model=64'. e.g. the bjam command line for vstudio 2008 will be then:
 
bjam toolset=msvc-9.0 variant=debug threading=multi link=shared define=_BIND_TO_CURRENT_VCLIBS_VERSION address-model=64
GeneralRe: Boost for 64-Bit Windows 7memberLegor22-Aug-12 1:33 
I really wonder why anyone downvotes this answer since it solves the OPs question exactly.
GeneralEasy way to build boost for VS 2008memberroel_1-Sep-08 3:45 
Download boost source & bjam binary from boost website. Unzip source. Go to root directory in command shell. Type and run
 
bjam --build-type=complete stage
 
Results will be in the stage/lib subdirectory of your boost source directory. Point your VS library include directory to there and you're good to go, for all configurations (static/dynamic linking, debug/release, multi/single threaded runtime, ...)
Questioncan this article be updatedmembermeirab18-Dec-07 12:48 
i have vc++2005(studio) on win/xp (r2)
and the build fails - maybe the author can try the build with bjam with this newer ide?
Answer( solution for vc++ 2005(studio) boost/regex 1.3.41 )membermeirab20-Dec-07 6:32 
Smile | :) ( solution for vc++ 2005(studio) boost/regex 1.3.41 )
 
download: boost_1_34_1_setup.exe
from:
http://www.boost-consulting.com/products/free
 
and let it do what it does.
 
after: move to: ----> C:\Program Files\boost\boost_1_34_1\libs\regex\build
 
copy file vc8.mak ----> vc81.mak
 
inside vc81.mak change string: "-1_34" to: "-1_34_1"
 
and NOW:
 
nmake -fvc81.mak
 
at this point you can see that at sub-dir /vc80 ---> files + libraries are created.
 
at end:
 
copy the: libboost_regex-vc80-mt-gd-1_34_1 (extension object file library)
to:
C:\Program Files\boost\boost_1_34_1\lib
 
NOW: move to vc++2005(studio)
 
at the properties of the project:
 
Properties ---> Linker ---> Additional Library Directories
 
"c:\Program Files\boost\boost_1_34_1\lib"
 
and that's all !!!
AnswerRe: can this article be updatedmemberGast12828-Aug-08 10:38 
I ve recently even build Boost 1.35 with visual studio 2008. I could upload the build script ifn't it was so painful to update one's own article...
GeneralRe: can this article be updatedmemberalexquisi25-Feb-09 2:09 
Any plans to update your article?
 
thanks,
 
Alex
GeneralRe: can this article be updatedmemberGast12825-Feb-09 7:26 
At home I use vstudio 2003 and Boost 1.38. The script line is:
 
bjam toolset=msvc-7.1 variant=debug threading=multi link=shared
 
At work I use vstudio 2008 and Boost 1.36. The script line is:
 
bjam toolset=msvc-9.0 variant=debug threading=multi link=shared
QuestionHow to compile 64-bit serialization library with VS2005?memberoleg639-Oct-07 5:50 
Does anybody tried to compile the 64-bit(x64) libraries on VS2005?

AnswerRe: How to compile 64-bit serialization library with VS2005?memberLars Schouw1-Dec-09 22:41 
Under VS2008 try this... seems to work.
 
bjam --build-dir=c:\boost --build-type=complete --toolset=msvc-9.0 address-model=64 architecture=x86 --with-system --with-serialization -j4
 
Good luck,
Lars
QuestionBoost: Where the DLLs are getting created?memberSarath.3-Feb-07 3:26 
I built the boost library.
as you said, it created bin directory inside the root directory.
but there are folders are existing as you said. but the dlls and lib files are not existing under these folders. Where I went wrong?
 
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin


AnswerRe: Boost: Where the DLLs are getting created?memberGast1284-Feb-07 3:02 
Did you search in this bin directory for mt-gd-1_33?
 
For example on my computer the date time library is located at:
'<boost root>\Boost\bin\boost\libs\date_time\build\boost_date_time.dll\vc-7_1\debug\threading-multi\boost_date_time-vc71-mt-gd-1_33.dll.
 

GeneralRe: Boost: Where the DLLs are getting created?memberSarath.4-Feb-07 4:26 
yea... I could see the directories there but there's no dlls or lib are created. Just blank folders.
 
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 18 Jul 2007
Article Copyright 2005 by Gast128
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid