...one of the most highly
regarded and expertly designed C++ library projects in the
world. — Herb Sutter and Andrei
Now the first thing you'd want to do is to build the Hello World module and try it for yourself in Python. In this section, we will outline the steps necessary to achieve that. We will use the build tool that comes bundled with every boost distribution: bjam.
Building without bjam
Besides bjam, there are of course other ways to get your module built. What's written here should not be taken as "the one and only way". There are of course other build tools apart from bjam.
Take note however that the preferred build tool for Boost.Python is bjam. There are so many ways to set up the build incorrectly. Experience shows that 90% of the "I can't build Boost.Python" problems come from people who had to use a different tool.
We will skip over the details. Our objective will be to simply create the hello world module and run it in Python. For a complete reference to building Boost.Python, check out: building.html. After this brief bjam tutorial, we should have built the DLLs and run a python program using the extension.
The tutorial example can be found in the directory: libs/python/example/tutorial. There, you can find:
The hello.cpp file is our C++ hello world example. The Jamroot is a minimalist bjam script that builds the DLLs for us. Finally, hello.py is our Python program that uses the extension in hello.cpp.
Before anything else, you should have the bjam executable in your boost directory or somewhere in your path such that bjam can be executed in the command line. Pre-built Boost.Jam executables are available for most platforms. The complete list of Bjam executables can be found here.
Here is our minimalist Jamroot file. Simply copy the file and tweak use-project boost to where your boost root directory is and your OK.
The comments contained in the Jamrules file above should be sufficient to get you going.
bjam is run using your operating system's command line interpreter.
Start it up.
A file called user-config.jam in your home directory is used to configure your tools. In Windows, your home directory can be found by typing:
into a command prompt window. Your file should at least have the rules for your compiler and your python installation. A specific example of this on Windows would be:
# MSVC configuration using msvc : 8.0 ; # Python configuration using python : 2.4 : C:/dev/toolsPython ;
The first rule tells Bjam to use the MSVC 8.0 compiler and associated tools. The second rule provides information on Python, its version and where it is located. The above assumes that the Python installation is in C:/dev/tools/Python/. If you have one fairly "standard" python installation for your platform, you might not need to do this.
Now we are ready... Be sure to cd to libs/python/example/tutorial where the tutorial "hello.cpp" and the "Jamroot" is situated.
It should be building now:
cd C:\dev\boost\libs\python\example\tutorial bjam ...patience... ...found 1101 targets... ...updating 35 targets...
And so on... Finally:
Creating library path-to-boost_python.dll Creating library path-to-'''hello_ext'''.exp **passed** ... hello.test ...updated 35 targets...
Or something similar. If all is well, you should now have built the DLLs and run the Python program.
Starting from Boost 1.35, bjam erases the generated executables (e.g. pyd file) after the test has concluded to conserve disk space. To keep bjam from doing that, pass --preserve-test-targets to bjam.
There you go... Have fun!