Sunday, November 27, 2011

Visual C++ 11 and Boost

Visual Studio / C++ 11 is currently at the Developer Preview level and as such doesn't have official support within libraries such as boost, even in the 1.48 release that came out in mid November 2011.
Despite this, the two will work together out of the box pretty much as is, with only a few warnings if you need to bootstrap in order to build the various libraries (for the non-header-only libraries, such as boost filesystem, etc).

Default Behaviour:

With a vanilla boost 1.48, after executing your VS vcvarsall.bat and running the boost bootstrap, you should get a successful build, with the following warnings:

bootstrapping issues:
cl : Command line warning D9035 : option 'GZ' has been deprecated and will be removed in a future release
cl : Command line warning D9036 : use 'RTC1' instead of 'GZ'
cl : Command line warning D9002 : ignoring unknown option '/MLd'

RTC1 is run time error checks.
GZ = Enable Stack Frame Run-Time Error Checking.

Why do these occur?  Turns out the boost batch files attempt to determine what your current compiler is, and with VC 11 it partially fails.

Cause of Warnings:

To begin with, the bootstrap calls into the build.bat file in
tools\build\v2\engine\

One of the first things this batch file tries to do is guess the toolset based on the environment variables set by visual studio.  You can find this in section :Guess_Toolset.
It starts with VC 10 and works its way down.  These all fail until eventually it finds that at least there's a VCINSTALLDIR, which sets it to VC7 mode (Visual C++ .NET 2002).

In and of itself, that's not as bad as it seems, because it's still calling cl.exe, which is the VC 11 compiler.  The issue is that later, down in the :Config_Toolset section it finds that we're using a VC 7 compiler and so passes the compiler a set of arguments suitable for VC 7, generating the warnings listed above.

Elimination of Warnings:

There are two steps we need to take to eliminate the warnings.

First we need to support VC11 in the :Guess_Toolset section.  It's just a minor copy and tweak from the VC 2010 detection code that we add before it, with VS100 changed to VS110 and vc10 updated to vc11:

call :Clear_Error
if NOT "_%VS110COMNTOOLS%_" == "__" (
    set "BOOST_JAM_TOOLSET=vc11"
    set "BOOST_JAM_TOOLSET_ROOT=%VS110COMNTOOLS%..\..\VC\"
    goto :eof)

Second, we need support down in the :Config_Toolset in order to pass the correct parameters to the compiler.
The :Skip_VC10 section leads into the test for the borland compiler, but of course that needs to be changed to :Skip_VC11, as we add in a new section there for :Skip_VC10 which verifies we have VS 11 installed and sets the appropriate environment settings, else jumps to the :Skip_VC11 section (the borland test) to continue as before.

The new section is once more just a minor copy and tweak from the existing VC 2010 code, and is included below for completeness:

:Skip_VC10
if NOT "_%BOOST_JAM_TOOLSET%_" == "_vc11_" goto Skip_VC11
if NOT "_%VS110COMNTOOLS%_" == "__" (
    set "BOOST_JAM_TOOLSET_ROOT=%VS110COMNTOOLS%..\..\VC\"
    )
if "_%VCINSTALLDIR%_" == "__" call :Call_If_Exists "%BOOST_JAM_TOOLSET_ROOT%VCVARSALL.BAT" %BOOST_JAM_ARGS%
if NOT "_%BOOST_JAM_TOOLSET_ROOT%_" == "__" (
    if "_%VCINSTALLDIR%_" == "__" (
        set "PATH=%BOOST_JAM_TOOLSET_ROOT%bin;%PATH%"
        ) )
set "BOOST_JAM_CC=cl /nologo /RTC1 /Zi /MTd /Fobootstrap/ /Fdbootstrap/ -DNT -DYYDEBUG -wd4996 kernel32.lib advapi32.lib user32.lib"
set "BOOST_JAM_OPT_JAM=/Febootstrap\jam0"
set "BOOST_JAM_OPT_MKJAMBASE=/Febootstrap\mkjambase0"
set "BOOST_JAM_OPT_YYACC=/Febootstrap\yyacc0"
rem change it back because VC11 is not officially known.
set "BOOST_JAM_TOOLSET=msvc"
set "_known_=1"

After that you should be able to bootstrap warning-free.   
   
ps. It's amusing how often I see reference to the file "boostrap.bat"

4 comments:

  1. There is a small typo in your code: your wrote mscv instead of msvc

    ReplyDelete
  2. I also had to use these small changes:
    http://article.gmane.org/gmane.comp.lib.boost.devel/228596/match=vc11

    ReplyDelete
  3. Thanks for the correction, hmuelner. Fixed now.

    Thanks for the link, Bob. I had seen it beforehand, but it didn't include some of the other changes I needed to make in the follow article. Were there any other vc10 -> vc11 additions I missed?

    ReplyDelete
  4. Thanks for this - useful articles

    ReplyDelete