Introduction

In the previous blog post here we discussed how to setup QuantLib using Boost 1.66, in 32-bit Visual Studio 2017. That was 5 years ago and a few things have changed!

In this post we will be setting up QuantLib using Boost 1.80 in 64-bit Visual Studio 2022 in Windows 11.

TL;DR

  1. Install Visual Studio 2022 (64-bit).
  2. Install the C++ Developer Workload, and cross-check that you have the minimum required optional installations (see list below). Remember your MSVC Toolkit version number (e.g. 14.3).
  3. Download the correct version of Boost that matches your MSVC Toolkit version number (e.g. 14.3).
  4. Use Visual Studio 2022 Developer Command Prompt to Bootstrap Boost.
  5. Check/edit your Boost project-config.jam file to ensure it points to the correct/existing cl.exe.
  6. Use Visual Studio 2022 Developer Command Prompt to Build Boost by running b2.exe.
  7. Download the latest QuantLib project/solution. Open the QuantLib.sln into Visual Studio 2022.
  8. Add new Property Sheets to each of the QuantLib components in property manager.
  9. Add the Boost directories and libraries to the Property Sheets’ VC++ Directories, C/C++ & Linker properties.
  10. Build the QuantLib Solution in Release-x64 mode.
  11. Run the QuantLib Unit Tests in Test Explorer.
  12. Done! Start using it.

Installing Visual Studio 2022

You will need to go to Microsoft and follow the instructions here and you’ll have VS2022 installed in no time. The new Installer application makes it easy to add and remove components of VS2022.

Visual Studio 2022 Workloads for C++ Development

It is important to note that you will need to install VS2022 to support a C++ Developer. This means that when it comes time to choose your workloads you must follow this separate guide here.

Fig 1 – Ensure that your select, download and install the desktop development with C++; and that you have the selected individual components (on the right, above) installed.

In particular, you must ensure that you have the following optional components installed:

  1. Windows 10 SDK (10.0)
  2. C++ CMake tools for WIndows
  3. MSVC v143 – VS 2022 C++ x64/x86 build tools
  4. MSVC v143 – VS 2022 C++ ARM64 build tools
  5. C++ Modules for v143 build tools (x64/x86)

These toolkit version number will be matches against the Boost build later on!

Determine your MSVC Toolset

For the installation of Boost (below) you will need to determine what version toolset your Microsoft Visual C++ (or MSVC) is using. To do this, open the Visual Studio Installer from your start menu, and click on the modify button. Then, under the individual components tab, search for “MSVC”:

Fig 2 – Determining your Microsoft Visual Studio Toolset version.

As you can see, I am currently running MSVC v14.3. We are now ready to download Boost.

Installing Boost 1.81

I am running 64-bit Windows 11, so this will dictate what version of Boost I download and install. Go to the Boost Artefactory website here.

To obtain the correct Boost library, you will need to go to the list of binaries here. As you can see from the list below,

Fig 3 – Choose your Boost binary based on your MSVC Toolset.

I will be downloading boost_1_81_0-msvc-14.3-64.exe because this version of Boost matches my 64-bit Windows and my MSVC Toolset. Read more about how Boost uses the MSVC here.

Click to download.

Once it has finished downloading, run the executible to unpack the files to a folder (preferrably one with no spaces or special characters in the full path, because this may need to go into your Environment Variables PATH).

Bootstrapping Boost

Open Visual Studio 2022 into any project and click on the ribbon menu: Tools > Command Line > Developer Command Prompt. In the command prompt, change directories to your boost folder containing the bootstrap.bat file. Run the batch file by entering the command ‘bootstrap.bat‘. Wait for it to finish.

You will now have a new executible file in the same directory called b2.exe. Execute this program in the command line by entering ‘b2.exe‘. Wait for it to finish.

Fig 4 – Boost Bootstrapping is completed succesfully.

Boost Ensure Boost Can See Your C++ Compiler

After bootstrapping Boost you will have a jam file called ‘project-config.jam‘. Open this up in a text editor.

Ensure that the line with using msvc is pointing to the cl.exe inside your Visual Studio 2022. You should be able to find your C++ compiler executable ‘cl.exe’ file in the following directory:

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64
Fig 5 – Ensure that Boost can find your Visual Studio 2022 cl.exe file.

If you have to edit it, remember to save and close.

Build Boost

Building Boost will, among many other things, build the Boost libraries (the .lib files) for C++. In particular, this step will generate the libboost_unit_test_framework-vc143-mt-gd-x64.lib library, which is used by the QuantLib testsuite.

Now you can compile Boost 1.81 on the command line with the following options:

b2.exe toolset=msvc-14.3 address-model=64

Wait for it to finish, you should get a successful message:

Fig 6 – Boost for C++ has now been successfully built!

Here is the same task being asked about at Stack Overflow. Close and re-open Visual Studio.

You should now be able to see the library files if you search Boost:

Fig 7 – The libboost_unit_test_framework-vc143-mt-gd-x64-1_81.lib files now exist.

Installing QuantLib

I dowloaded the latest version (v1.31.1) of QuantLib from their GitHub site here. Simply open QuantLib.sln into Visual Studio 2022 (64-bit).

Click to make the project Release mode and x64.

The Property Manager

Now open the Property Manager by navigating to the Ribbon Menu: View > Other Windows > Property Manager.

Once the window pops up it should become the third or fourth tab in your Solution Explorer pane (along with Properties). If not, then drag it to the left pane.

Property Manager is very important for what we are about to do. I originally mistook Solution Explorer for Property Manager and got stuck in a heap of trouble. So make sure you perform the next set of operations on Property Manager.

Adding New Property Sheets

Expand QuantLib in the Property Manager. We will need to add a Property Sheet to each component. Select all the QuantLib components, right-click and then Add New Project Property Sheet…

Fig 8 – Adding a Property Sheet to each QuantLib component.

Now expand each component of QuantLib in the Property Manager, and Ctrl+click on each Property Sheet, then click Properties:

Fig 9 – Simultaneously editing the new property sheets of each QuantLib component.

Because we ctrl+clicked, edit properties, we only need to do the following once. In the Property Page pop-up window, under common properties do the following:

  1. VC++ Directories | Include Directories: Add path to …\boost\boost_1_81_0
  2. VC++ Directories | Library Directories: Add path to …\boost\boost_1_81_0\libs
  3. C/C++ | General | Additional Include Directories: Add path to …\boost\boost_1_81_0\boost
  4. Linker | General | Additional Library Directories: Add path to …\boost\boost_1_81_0\libs

Building the QuantLib Solution

Close the Property Manager and go to the Solution Explorer.

Right-click on the QuantLib solution, and click Properties. Add the exact same paths to this solution, click OK, and wait for it to save. Then right-click build.

Right-click on the testsuite solution, and click Properties. Add the exact same paths to this solution, click OK, and wait for it to save. Then right-click build.

If both solutions build without error, then you can now proceed to rght-click on the Solution ‘QuantLib’ and click build solution.

Fig 10 – Build the entire QuantLib Solution.

Running QuantLib Unit Tests

Once the QuantLib solution (QuantLib, Examples and TestSuite) builds without error, it’s time to run the Unit Tests.

Go to the Test Explorer panel and click the green double-arrow Run All button. All tests should pass:

Fig 11 – Run All QuantLib Unit Tests

We are now ready to start using QuantLib!

Using QuantLib in VS2022 64-bit For the First Time

For my first usage of QuantLib, I will be creating a new Visual Studio Project. I will be creating a GoogleTest Project. To do so, open up Visual Studio 2022 and click File > New > Project. Search for “C++” and scroll down until you find Google Test project template:

Fig 12 – Creating a new Google Test project.

This project will come with precompiled headers, so the file pch.hpp should contain the include for gtest already.

When the project opens create a new filter called InstrumentTests and add a new cpp file to it called TestEuropeanOption.cpp.

Fig 13 – Starting point for a new QuantLib GoogleTest project.

Adding Directories

You will need to right-click on the solution and add the followinig directories:

  1. VC++ Directories | Include Directories: Add path to …\QuantLib\QuantLib-1.31.1
  2. VC++ Directories | Include Directories: Add path to …\boost\boost_1_81_0
  3. VC++ Directories | Library Directories: Add path to …\QuantLib\QuantLib-1.31.1\lib
  4. VC++ Directories | Library Directories: Add path to …\boost\boost_1_81_0\libs
  5. C/C++ | General | Additional Include Directories: Add path to …\boost\boost_1_81_0
  6. Linker | General | Additional Library Directories: Add path to …\boost\boost_1_81_0\libs

Test European Option Build

Enter the following code into TestEuropeanOption.cpp:

#include "pch.h"

TEST(TestInstruments, TestVanillaOption) {

	// Create a Calendar obj
	QuantLib::Calendar calendar = QuantLib::TARGET();

	// Create a Date objs
	QuantLib::Date todaysDate(25, QuantLib::September, 2023);
	QuantLib::Date settlementDate(29, QuantLib::December, 2023);
	QuantLib::Date maturityDate(31, QuantLib::December, 2023);

	// Create a Call Option obj
	QuantLib::Option::Type optionType(QuantLib::Option::Call);

	// Create Option params
	QuantLib::Real underlyingPrice = 100.0;
	QuantLib::Real strike = 90.0;  // Out of the money
	QuantLib::Spread dividendYield = 0.00;
	QuantLib::Rate interestRate = 0.05;
	QuantLib::Volatility volatility = 0.2;

	// Create the Payoff objs
	boost::shared_ptr<QuantLib::Exercise> europeanExercise(new QuantLib::EuropeanExercise(maturityDate));
	boost::shared_ptr<QuantLib::StrikedTypePayoff> payoff(new QuantLib::PlainVanillaPayoff(optionType, strike));

	// Create the European Option Instrument
	QuantLib::VanillaOption europeanOption(payoff, europeanExercise);

	// Calculate the NPV
	//double npv = europeanOption.NPV();  // We need a pricing engine before we can test this!

	EXPECT_EQ(1, 1);
	EXPECT_TRUE(true);
}

Hit Ctrl+F7 to compile the file.

When it succeeds you can then build the test solution.

Running the Test Explorer

With the test solution successfully built you can now go to the Test Explorer and click Run All:

Fig 14 – Using Test Explorer to check QuantLib methods are working properly.

This concludes the blog. In the next blog we will look at using QuantLib to build a pricing engine so that we can price our European options.

Troubleshooting

Visual Studio Errors

Share or reuse Visual Studio project settings

Visual Studio 2022 no longer supports .user files. Click here to learn about it.

QuantLib Build Errors

Cannot Open Include File: ‘includedFile.hpp’; No Such File or Directory

Since we are compiling the Release x64 please remember to switch from debug to release mode.

LNK1104 Error – Cannot Open LibBoost

The first error I encountered was: LNK1104 ‘cannot open ‘libboost_unit_test_framework-vc143-mt-x64-1_66.lib’. This means that:

  1. You forgot to build Boost. This act of building Boost (by running b2.exe) generates these libboost library files, or
  2. You built boost, but you built the wrong one with the wrong toolset and the library files are the wrong kind. Remember, that your Boost project-config.jam file points to your Visual Studio 2022 (64-bit) cl.exe and contains the MSVC toolset number version (e.g. 14.3). All of these numbers must align during the Boost build step!

Here is a Stack Overflow thread discussing this issue.

LNK1104 Error – Cannot Open QuantLib-x64-mt-gd.lib

The second error I encounted was: LNK1104 ‘cannot open ‘QuantLib-x64-mt-gd.lib’. See the ‘gd‘ suffix? This means that you haven’t built the QuantLib solution in debug mode! Go back to QuantLib Solution and repeat the Property steps above for the debug version of the build (yes, you will have to add all the Boost directories again…).

References

  1. https://www.quantlib.org/
  2. Building QuantLib in VS2017.
  3. https://boostorg.jfrog.io/artifactory/main/release/1.81.0/binaries/
  4. https://github.com/lballabio/QuantLib/releases/tag/v1.31.1
  5. https://learn.microsoft.com/en-us/cpp/build/vscpp-step-0-installation?view=msvc-170
  6. https://learn.microsoft.com/en-us/cpp/build/create-reusable-property-configurations?view=msvc-170&viewFallbackFrom=vs-2019
  7. https://stackoverflow.com/questions/41464356/build-boost-with-msvc-14-1-vs2017-rc
  8. https://stackoverflow.com/questions/470840/how-do-i-resolve-lnk1104-error-with-boost-filesystem-library-in-mscv
  9. https://www.boost.org/doc/libs/1_32_0/tools/build/v1/msvc-tools.html
  10. https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B