Python and C meet Boost

In the previous post we created C library and used it in Python code. And you noticed how much time we spend on memory management. Is there a way to make our life easier? Yes, the an answer could be boost.

Those who do serious development in C/C++ know boost. Boost provides free peer-reviewed portable C++ source libraries. And one of them is boost-python, it’s a wrapper for all level stuff we did last time.

Boost provides good documentation so I am not going to repeat it, just brief summary:

  1. get boost. Current version is 1.76
  2. unzip it in a root folder: C:\boost_1_76_0
  3. open command prompt and go to that folder
  4. execute:
bootstrap
b2.exe

You might heard that you don’t have to build boost to use it, it’s header only library. This is partially true – majority of boost libraries are header only, but there are some exceptions and boost-python is one of them.

Once boost is build (which might take a while), get my sample project.

Also you might want to look at the sample projects shipped with boost. They are located at C:\boost_1_76_0\libs\python\example\. To build them boost recommend using bjam utility. It is located at C:\boost_1_76_0\tools\build\src\engine\. I would recommend copying it to boost root folder: C:\boost_1_76_0\. Next you will need add bjam configuration file at you home directory: C:\Users\<user name>. Create file named user-config.jam:

#  MSVC configuration
using msvc : 14.2 ;

#  Python configuration
using python : 3.9 : C:\\Python\\Python39 ;

Change msvc version to version you have, mine correspond to VS 2019. And change to actual Python path. Now to build examples, go to some of the examples folder and run:

C:\boost_1_76_0\bjam.exe --debug-configuration

also in my case I have to add <include> parameter in Jamfile file (in the examples folder)

project tutorial
  : requirements
    <include>../../../..
    <location>.
    ;

To be continued…

Leave a Reply