C++ - Boost - Building the Boost.Python NumPy extension as a library

If you are a scientist and interested in Python, you certainly already know the NumPy package.

In this tutorial I'll propose to explain how to install it on Windows in order to be used with the Boost.Python library.

We'll also make an Hello world example.

First of all

We need to install Python 3 and Boost on your computer.

So in order to have the exact same software and libraries installed in the exact same locations, I suggest to follow the 2 following tutorials:

  1. Pythonhttps://www.badprog.com/python-3-installing-on-windows-10
  2. Boosthttps://www.badprog.com/c-boost-setting-up-on-windows-10


Installing NumPy

Numpy was previously a library from Boost.

But with some recent changes it's not the case anymore.

Indeed NumPy has become a Boost.Python extension.

But to use it you'll have to build it as a library.

Yes it's clearly confuse, don't worry.

And for going further if you want to use it from the Boost binaries you won't be able to.

The binaries don't come with NumPy already installed.

It means that if you try to include the Numpy extension headers directly on your code you'll encounter the following error:

  • Error LNK1104 cannot open file 'boost_numpy37-vc141-mt-gd-x64-1_71.lib'

So how does it work then?

The answer is that you MUST download and install the Numpy package on your computer before using it.

Indeed this package isn't directly available from Python nor from the built Boost libraries that you have installed.

It's a Python package so we have to use the Package Installer for Python (pip).

Thus to get the NumPy package, just open a console from the directory of your choice then type the following command:

pip install numpy

You'll have this message and the NumPy package will start to download and install on your computer:

PS D:\dev\c++\boost\BadprogTutorial> pip install numpy

Collecting numpy

  Downloading https://files.pythonhosted.org/packages/34/40/c6eae19892551ff91bdb15a884fef2d42d6f58da55ab18fa540851b48a32/numpy-1.17.4-cp37-cp37m-win_amd64.whl (12.7MB) 12.7MB

Installing collected packages: numpy

Successfully installed numpy-1.17.4

Great, NumPy is now installed on your computer.

Creating the Boost.Python NumPy extension library

Let's continue by building the NumPy extension from Boost.Python in order to get a NumPy library.

For that we are going to use the b2 tool (already installed from the previous Boost tutorial above).

Let's go in the following directory:

  • C:\soft\boost_1_71_0\libs\python\build\

In this directory there is only one file:

  • Jamfile

We don't have to modify it, we just have to use it.

So open a console from this directory and type:

b2 address-model=64 release debug

If it doen't work try with:

..\..\..\b2.exe address-model=64 release debug

The b2 is building your libraries and you can see this at the beginning of the message:

PS C:\soft\boost_1_71_0\libs\python\build> ..\..\..\b2.exe address-model=64 release debug

Performing configuration checks

    - default address-model    : 32-bit

    - default architecture     : x86



...found 3362 targets...

...updating 97 targets...

Don't worry about the 32-bit default address-model and the x86 default architecture, it's a bug.

Indeed your x64 libraries have been successfully built.

To check that let's go at these locations to find the debug and release DLLs and LIBs:

  • C:\soft\boost_1_71_0\bin.v2\libs\python\build\msvc-14.1\debug\address-model-64\python-3.7\threading-multi\
  • C:\soft\boost_1_71_0\bin.v2\libs\python\build\msvc-14.1\release\address-model-64\python-3.7\threading-multi\

From these 2 directories, copy and paste the following NumPy DLLs and LIBs:

  • boost_numpy37-vc141-mt-gd-x64-1_71.dll
  • boost_numpy37-vc141-mt-gd-x64-1_71.lib
  • boost_numpy37-vc141-mt-x64-1_71.dll
  • boost_numpy37-vc141-mt-x64-1_71.lib

To the following directory:

  • C:\soft\boost_1_71_0\lib64-msvc-14.1\

So you have now this:

  • C:\soft\boost_1_71_0\lib64-msvc-14.1\boost_numpy37-vc141-mt-gd-x64-1_71.dll
  • C:\soft\boost_1_71_0\lib64-msvc-14.1\boost_numpy37-vc141-mt-gd-x64-1_71.lib
  • C:\soft\boost_1_71_0\lib64-msvc-14.1\boost_numpy37-vc141-mt-x64-1_71.dll
  • C:\soft\boost_1_71_0\lib64-msvc-14.1\boost_numpy37-vc141-mt-x64-1_71.lib

Here we go we are ready to use the NumPy Boost.Python extension.

Visual Studio

Creating a new project

From Visual Studio > File > New > Project... > Installed > Visual C++ > Windows Desktop > Windows Console Application.


  • Name: BadprogNumpyTutorial
  • Location: C:\dev\c++\boost\

So you have now the following directory:

  • C:\dev\c++\boost\BadprogNumpyTutorial\

Be sure to set the include and library directories as suggested in the above links (tutorials for Boost and Python).


Then type the following code:

// badprog.com

#include "pch.h"

#include <boost\python.hpp>
#include <boost\python\numpy.hpp>

#include <iostream>

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
int main() {

    std::cout << "Hello from Badprog and NumPy :D" << std::endl; 


Build and run

If you don't have the DLLs copied in the Boost directory, you'll have this error:

  • The code execution cannot proceed because boost_numpy37-vc131-mt-gd-x64-1_71.dll was not found.
    Reinstalling the program may fix this problem.

Otherwise everything will go smoothly and you'll see:

Hello from Badprog and NumPy :D


One more step towards the understanding of Boost and Python.

NumPy is now ready to be used.

Good job you did it. laugh



Thanks for the great article, this one helped alot! I would like to suggest a little change: you mentioned the numpy extension build command "b2 address-model=64 release debug". I think you should add "threading=multi" option. So the command will be as follows: "b2 address-model=64 threading=multi release debug". This option will build lib with "mt" tag.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.