| Home | Libraries | People | FAQ | More

## Building Hello World

## From Start To Finish

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 shall outline the steps necessary to achieve that. We shall 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 shall 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 two DLLs:

- boost_python.dll
- hello.pyd

if you are on Windows, and

- libboost_python.so
- hello.so

if you are on Unix.

The tutorial example can be found in the directory: `libs/python/example/tutorial`. There, you can find:

- hello.cpp
- Jamfile

The `hello.cpp` file is our C++ hello world example. The `Jamfile` is a minimalist bjam script that builds the DLLs for us.

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.

## Let's Jam!

Here is our minimalist Jamfile:

    # This is the top of our own project tree
    project-root ;

    import python ;

    extension hello                     # Declare a Python extension called hello
    :   hello.cpp                       # source
        # requirements and dependencies for Boost.Python extensions
        <template>@boost/libs/python/build/extension
        ;

First, we need to specify our location. You may place your project anywhere. `project-root` allows you to do that.

    project-root ;

By doing so, you'll need a Jamrules file. Simply copy the one in the example/tutorial directory and tweak the `path-global BOOST_ROOT` to where your boost root directory is. The file has detailed instructions you can follow.

Then we will import the definitions needed by Python modules:

    import python ;

Finally we declare our `hello` extension:

    extension hello                     # Declare a Python extension called hello
    :   hello.cpp                       # source

        # requirements and dependencies for Boost.Python extensions
        <template>@boost/libs/python/build/extension
        ;

The last part tells BJam that we are depending on the Boost Python Library.

## Running bjam

bjam is run using your operating system's command line interpreter.

> Start it up.

Make sure that the environment is set so that we can invoke the C++ compiler. With MSVC, that would mean running the `Vcvars32.bat` batch file. For instance:

    C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat

Some environment variables will have to be setup for proper building of our Python modules. Example:

    set PYTHON_ROOT=c:/dev/tools/python
    set PYTHON_VERSION=2.2

The above assumes that the Python installation is in `c:/dev/tools/python` and that we are using Python version 2.2. You'll have to tweak these appropriately.

Be sure not to include a third number, e.g. not "2.2.1", even if that's the version you have.

Take note that you may also do that through the Jamrules file we put in our project as detailed above. The file has detailed instructions you can follow.

Now we are ready... Be sure to `cd` to `libs/python/example/tutorial` where the tutorial `"hello.cpp"` and the `"Jamfile"` is situated.

Finally:

    bjam -sTOOLS=vc-7_1

We are again assuming that we are using Microsoft Visual C++ version 7.1. If not, then you will have to specify the appropriate tool. See Building Boost Libraries for further details.

It should be building now:

    cd C:\dev\boost\libs\python\example\tutorial
    bjam -sTOOLS=msvc
    ...patience...
    ...found 1703 targets...
    ...updating 40 targets...

And so on... Finally:

    Creating library bin\boost\libs\python\build\boost_python.dll\vc-7_1\debug\th
    reading-multi\boost_python.lib and object bin\boost\libs\python\build\boost_pyth
    on.dll\vc-7_1\debug\threading-multi\boost_python.exp
    vc-C++ bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.obj
    hello.cpp
    vc-Link bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.pyd bin\tutori
    al\hello.pyd\vc-7_1\debug\threading-multi\hello.lib
       Creating library bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.li
    b and object bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.exp
    ...updated 31 targets...

If all is well, you should now have:

- boost_python.dll
- hello.pyd

if you are on Windows, and

- libboost_python.so
- hello.so

if you are on Unix.

`boost_python.dll` and `hello.pyd` can be found somewhere in your project's `bin` directory. After a successful build, you make it possible for the system to find boost_python.dll or libboost_python.so (usually done with LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, or some other variable on *nix and with PATH on Windows) and for Python to find the hello module (Done with PYTHONPATH on all systems.)

You may now fire up Python and run our hello module:

    >>> import hello
    >>> print hello.greet()
    hello, world

> There you go... Have fun!

| Copyright © 2002-2005 Joel de Guzman, David Abrahams