Revision 8 as of 2003-10-28 21:43:11

Clear message

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 [http://www.boost.org/libs/python/doc/tutorial/doc/building_hello_world.html 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
   Example.cpp
   # dependencies
   <dll>../build/boost_python
  
:  # requirements
   <include>[[FULL_PATH_INCLUDE_DIR]]
   <include>[[RELATIVE_PATH_INCLUDE_DIR]]
   <library-file>[[FULL_PATH_AND_LIBNAME]]
   <library-path>[[PATH_TO_LIB]]
   <library-file>[[LIBNAME]]
  ;

# Declare a test for the extension module
boost-python-runtest test1
    :  # Python test driver
    test1.py
    # 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 Windows IDE

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
    }
    else
    {
       libname = libfoo.a
    }

        ...

    <library-file>$(libname)

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)

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