Building Extensions with boost.python

Using bjam

bjam is a standard tool for building boost library itself. Thus it is preferable way to build Python extensions based on boost.python with bjam. Basic example listed in tutorial.

However if you want to add external libraries in your extension (that is why you use boost.python, isn't it?), you must add them to the Jamfile:

# NOTE: Change [[VARIABLES]] according to your system

# Specify our location in the boost project hierarchy
subproject libs/python/MyExtension ; ######## if you put your dir in boost hierarchy

# Include definitions needed for Python modules
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;

include python.jam ;

# Declare a Python extension
extension Example
:  # sources
   # dependencies
:  # requirements

# Declare a test for the extension module
boost-python-runtest test1
    :  # Python test driver
    # extension modules to use
    <pyd>Example ;

Keeping your projects under boost hierarchy is often inconvenient. You may build your extension from any place by:

Using make

Using SCons

You might want to try scons.

It's really easy to build python extensions with scons. Here is an example that would build

BOOST_VERSION = 'boost.cvs'
BOOST = '/usr/local/src/' + BOOST_VERSION
env = Environment (LIBPATH=['./',BOOSTLIBPATH], CPPPATH=[BOOST, '/usr/include/python2.3'],  
env.SharedLibrary (target='uvector', source='', SHLIBPREFIX='', LIBS=[BOOST_PYTHON_LIB])

Using Windows IDE

Using CMake

Save the file as CMakeLists.txt, cd into the directory, and run cmake . followed by make:

cmake_minimum_required(VERSION 2.6)

SET(ENV{BOOST_ROOT} "/path/to/my/boost")

#substitute your version number
find_package(Boost 1.48 EXACT REQUIRED COMPONENTS python)

INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "/path/to/my/python/include")

ADD_LIBRARY(MyLibrary SHARED MyLibraryInterface.cpp)

On Linux, this will make a library in the same directory.

Tips and tricks

To keep up with bjam rules you might want to have a dry run without actually building anything: {{{bjam -na }}}

To copy resulting executable to desired directory take a look at the stage rule.

To specify library in a platform-independent way you could do something like:

    local libname
    if $(NT)
       libname = foo.lib
       libname = libfoo.a



in the Jamfile.

Add -DBOOST_PYTHON_STATIC_LIB to your compiler command-line or <define>BOOST_PYTHON_STATIC_LIB to your bjam requirements; that will turn off exporting for win32: __declspec(dllexport).

If you are getting error C1055: compiler limit : out of keys on MS VisualC, try change /ZI (Program database for edit and continue) to /Zi (Program database).

If you are getting error error C1076: compiler limit - internal heap limit reached try add /ZmNNN (with NNN=300..800)

You can use a file if u are careful
from distutils.core import setup
from distutils.extension import Extension
import os.path
import sys
if sys.platform == "win32" :
    include_dirs = ["C:/Boost/include/boost-1_32","."]
else :
    include_dirs = ["/usr/include/boost-1_32","."]

files = ["test.cpp","itest.cpp"]


For the above I compile and install boost using bjam then you set the appropriate paths to your boost header and libs It works on linux and windows with mingw

That way u have nice cross platform build :)

boost.python/BuildingExtensions (last edited 2012-02-13 18:30:15 by jeener)

Unable to edit the page? See the FrontPage for instructions.