Memo‎ > ‎

Python + Boost: Compile Jamroot for boost.python

posted Apr 14, 2013, 8:07 PM by Teng-Yok Lee   [ updated Sep 15, 2013, 11:05 AM ]
This is some notes for my test.

I started with the sample in the tutorial libs/python/example/tutorial. It is fine to ruin bjam in that folder. Nevertheless, if the folder is copied to other place, bjam cannot work. Instead, I need to use b2 with additional options. Essentially, I need to specify the toolset and the root of boost source code.

b2 -sBOOST_ROOT=F:\software\boost_1_53_0 --toolset=msvc-10.0

  1. Always remember to add address-model=64 for 64-bit Python libraries.
  2. Although boost.python generate shared libraries, boost can be build as static libraries, which is what I did.
To define a C function for python, the function type and arguments are packed as a template class for the function def(). The following code is an simple example:

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

double test(int a, int b)
   return (double)(a + b);

    using namespace boost::python;
    def<double(int, int)>("test_func", test);

The python code that call this function will look like this:

import test_module
print test_module.test_func(1, 2)

The Jamroot (modified from the sample in the sample code of boost. The path is <boost_source_dir>/libs/python/example/tutorial. The credit belongs to by David Abrahams.)

# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at

# Modified by Teng-Yok Lee on 04/17/2013.

import python ;

if ! [ python.configured ]
    ECHO "notice: no Python configured in user-config.jam" ;
    ECHO "notice: will use default configuration" ;
    using python ;

# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.
use-project boost
  : F:/software/boost_1_53_0 ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.
  : requirements <library>/boost/python//boost_python
    : requirements <include>D:/mylib/install/vc2010/x64/include
    : requirements <include>D:/lib/install/vc2010/x64/include
# Declare the extension module.  You can specify multiple
# source files after the colon separated by spaces.
# Format: python-extension <module_name> : <module_source> ;
python-extension test_module : module_py.cpp ;

# Put the extension and Boost.Python DLL in the current directory, so
# that running script by hand works.
install convenient_copy
  : test_module
  : <install-dependencies>on <install-type>SHARED_LIB <install-type>PYTHON_EXTENSION

# A little "rule" (function) to clean up the syntax of declaring tests
# of these extension modules.
local rule run-test ( test-name : sources + )
    import testing ;
    testing.make-test run-pyd : $(sources) : : $(test-name) ;

# Declare test targets
run-test test_test : test_module ;
PS. The source code is modified from the original example by Joel de Guzman. The original codes can be seen here:

PS. A good article about passing arrays from Python to C: