[library python [version 1.0] [authors [de Guzman, Joel], [Abrahams, David]] [copyright 2002 2003 2004 2005 Joel de Guzman, David Abrahams] [category inter-language support] [purpose Reflects C++ classes and functions into Python ] [license Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt ) ] ] [/ QuickBook Document version 0.9 ] [def __note__ [$images/note.png]] [def __alert__ [$images/alert.png]] [def __tip__ [$images/tip.png]] [def :-) [$images/smiley.png]] [def __jam__ [$images/jam.png]] [section QuickStart] The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools -- just your C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that you should not have to change the C++ code at all in order to wrap it, making Boost.Python ideal for exposing 3rd-party libraries to Python. The library's use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative interface definition language (IDL). [h2 Hello World] Following C/C++ tradition, let's start with the "hello, world". A C++ Function: char const* greet() { return "hello, world"; } can be exposed to Python by writing a Boost.Python wrapper: #include using namespace boost::python; BOOST_PYTHON_MODULE(hello) { def("greet", greet); } That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session: [python] >>> import hello >>> print hello.greet() hello, world [c++] [:['[*Next stop... Building your Hello World module from start to finish...]]] [endsect] [section:hello Building Hello World] [h2 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]. [blurb __note__ [*Building without bjam]\n\n 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].\n\n 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 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 [@http://sourceforge.net/project/showfiles.php?group_id=7586 here]. [h2 Let's Jam!] __jam__ Here is our minimalist Jamfile: [pre # 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