How to use Boost in Visual Studio 2017

By , last updated July 31, 2018

After installing one of the versions of the C++ Boost library you need to know how to use it in VS2017. Remember, only Boost 1.64 and up works with Visual Studio 2017.

Property sheets

As for any of the previous versions of Boost (1.63 and down) the best way of using Boost with Visual Studio is to use property sheets.

Here is a property sheet for Boost version 1.64 for VS17:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros">
    <PropSheetPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</PropSheetPath>
  </PropertyGroup>
  <PropertyGroup />
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>$(PropSheetPath)boost_1_64_0\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>$(PropSheetPath)boost_1_64_0\stage\$(Platform)\lib\</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>

Save the property sheet in a file called boost_current.props. Don’t save the version of boost in the filename. By using a generic name for the property sheet, it’s easier to edit the property sheet itself for the next version of boost.

Another thing to remember. In the recent versions of Visual Studio, the default platform for 32-bit code is x86, not Win32. If you are using x86 as the platform for 32-bit code, then the libraries must be in stage/x86. The build script can be edited so this happens automatically.

Add an existing property sheet to VS2017

We have property sheets for several versions of the C++ Boost library at our Studiofreya Github page (.props files).

Open the property manager.

Add existing property sheet (the one you saved in the previous step).

Verify the property sheet is added for all configurations.

Use Boost in project files

The other “inferior” way is to add the Boost include and library paths to the project files itself. While this is quick and dirty, and gets the job done, it will make upgrading Boost a pain later. For solutions having many projects, all projects must reference the same installation of Boost. And all projects can have many configurations and architectures. Open properties for the project, and add include and library paths to your Boost installation.

Use the comment section if there are any questions.

Comments

  1. Mark May 19, 2017 Leave a Reply

    Hi, installed Boost and VS2017 (community) on Win10 as explained here.
    I have a problem with Boost.Test. When I compile example of and run it in console, the program wait for pressing any key and not produces output. I’ve tried install precompiled binaries or compile it as explained above. Program return non zero error code, but no output. What I did wrong?

    • Kent Fagerjord May 19, 2017 Leave a Reply

      Hi Mark,

      I made a new Empty Project and tried the sample you linked, and it builds and runs fine. I have Visual Studio 2017 Professional though, not Community.

      I get this output:

      Running 1 test case...
      boost-test.cpp(8): error: in "first_test": check i == 2 has failed
      [1 != 2]
      Press any key to continue . . .
      

      With this code:

      #define BOOST_TEST_MODULE My Test
      #include <boost/test/included/unit_test.hpp>
      
      BOOST_AUTO_TEST_CASE(first_test)
      {
        int i = 1;
        BOOST_TEST(i);
        BOOST_TEST(i == 2);
      
        system("pause");
      }

      Try placing breakpoints in the test project to see if the code is actually run.

      • Mark May 19, 2017 Leave a Reply

        @Kent Fagerjord I’ve open windows cmd and run compiled example.exe. It waits for input, but outputs nothing.
        Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved.
        C:\Users\Mark\Documents\Visual Studio 2017\Projects\example\example>example
        s
        C:\Users\Mark\Documents\Visual Studio 2017\Projects\example\example >

      • Mark May 19, 2017 Leave a Reply

        Ok, sorry, the problem solved by adding “system(“pause”)”.

  2. Michael Powell October 30, 2018 Leave a Reply

    Pretty swift approach. The only catch is it ties you to a file/directory in your system, whether by policy, convention, etc. There’s a dependency somehow, some way, I do not know of how to avoid this, really. I will add an environment variable to abstract this away somewhat, and carry the props file with my workspace. Pretty nice approach all things considered.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*