2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-23 17:52:17 +00:00
Files
python/doc/tutorial/doc/html/boost_python.hello.html
Aleksey Gurtovoy 59ca82128a c++boost.gif -> boost.png replacement
[SVN r25573]
2004-10-05 15:45:52 +00:00

131 lines
14 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title> Building Hello World</title><link rel="stylesheet" href="boostbook.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="Chapter 1. Boost.Boost Python 1.0"><link rel="up" href="index.html" title="Chapter 1. Boost.Boost Python 1.0"><link rel="previous" href="index.html" title="Chapter 1. Boost.Boost Python 1.0"><link rel="next" href="boost_python.exposing.html" title=" Exposing Classes"><link rel="chapter" href="index.html" title="Chapter 1. Boost.Boost Python 1.0"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table cellpadding="2" width="100%"><td valign="top"><img src="../../../../../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86"></td><td align="center"><a href="../../index.htm">Home</a></td><td align="center"><a href="libraries.html">Libraries</a></td><td align="center"><a href="../../people/people.htm">People</a></td><td align="center"><a href="../../more/faq.htm">FAQ</a></td><td align="center"><a href="../../more/index.htm">More</a></td></table><hr><div class="spirit-nav"><a accesskey="p" href="index.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="boost_python.exposing.html"><img src="images/next.png" alt="Next"></a></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="boost_python.hello"></a> Building Hello World</h2></div></div><div></div></div><a name="hello.from_start_to_finish"></a><h2><a name="id387283"></a>From Start To Finish</h2><p>
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: <span class="bold"><b>bjam</b></span>.</p><div class="informaltable"><table class="table"><colgroup><col></colgroup><tbody><tr><td><span class="inlinemediaobject"><img src="images/lens.gif"></span><span class="bold"><b>Building without bjam</b></span><p></p><p></p></td></tr></tbody></table></div><p>
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 <tt class="literal">bjam</tt>.</p><p>
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.
]</p><p>
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: <a href="../../building.html" target="_top">
building.html</a>.
After this brief <span class="emphasis"><em>bjam</em></span> tutorial, we should have built two DLLs:</p><div class="itemizedlist"><ul type="disc"><li>
boost_python.dll
</li><li>
hello.pyd
</li></ul></div><p>
if you are on Windows, and</p><div class="itemizedlist"><ul type="disc"><li>
libboost_python.so
</li><li>
hello.so
</li></ul></div><p>
if you are on Unix.</p><p>
The tutorial example can be found in the directory:
<tt class="literal">libs/python/example/tutorial</tt>. There, you can find:</p><div class="itemizedlist"><ul type="disc"><li>
hello.cpp
</li><li>
Jamfile
</li></ul></div><p>
The <tt class="literal">hello.cpp</tt> file is our C++ hello world example. The <tt class="literal">Jamfile</tt> is a
minimalist <span class="emphasis"><em>bjam</em></span> script that builds the DLLs for us.</p><p>
Before anything else, you should have the bjam executable in your boost
directory or somewhere in your path such that <tt class="literal">bjam</tt> can be executed in
the command line. Pre-built Boost.Jam executables are available for most
platforms. For example, a pre-built Microsoft Windows bjam executable can
be downloaded <a href="http://boost.sourceforge.net/jam-executables/bin.ntx86/bjam.zip" target="_top">
here</a>.
The complete list of bjam pre-built
executables can be found <a href="../../../../../tools/build/index.html#Jam" target="_top">
here</a>.</p><a name="hello.let_s_jam_"></a><h2><a name="id401473"></a>Let's Jam!</h2><p><span class="inlinemediaobject"><img src="images/jam.png"></span></p><p>
Here is our minimalist Jamfile:</p><pre class="programlisting"><tt class="literal"> subproject libs/python/example/tutorial ;</tt></pre><p>
[pre
subproject libs/python/example/tutorial ;</p><pre class="programlisting"><tt class="literal"><span class="identifier"> SEARCH</span><span class="identifier"> on</span><span class="identifier"> python</span><span class="special">.</span><span class="identifier">jam</span><span class="special"> =</span>#<span class="special">(</span><span class="identifier">BOOST_BUILD_PATH</span><span class="special">)</span><span class="special"> ;</span><span class="identifier">
include</span><span class="identifier"> python</span><span class="special">.</span><span class="identifier">jam</span><span class="special"> ;</span><span class="identifier">
extension</span><span class="identifier"> hello</span>#<span class="identifier"> Declare</span><span class="identifier"> a</span><span class="identifier"> Python</span><span class="identifier"> extension</span><span class="identifier"> called</span><span class="identifier"> hello</span><span class="special">
:</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">cpp</span>#<span class="identifier"> source</span><span class="special">
&lt;</span><span class="identifier">dll</span><span class="special">&gt;../../</span><span class="identifier">build</span><span class="special">/</span><span class="identifier">boost_python</span>#<span class="identifier"> dependencies</span><span class="special">
;</span></tt></pre><p>
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;</p><pre class="programlisting"><tt class="literal"><span class="identifier"> extension</span><span class="identifier"> hello</span>#<span class="identifier"> Declare</span><span class="identifier"> a</span><span class="identifier"> Python</span><span class="identifier"> extension</span><span class="identifier"> called</span><span class="identifier"> hello</span><span class="special">
:</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">cpp</span>#<span class="identifier"> source</span><span class="special">
&lt;</span><span class="identifier">dll</span><span class="special">&gt;../../</span><span class="identifier">build</span><span class="special">/</span><span class="identifier">boost_python</span>#<span class="identifier"> dependencies</span><span class="special">
;</span></tt></pre><p>
extension hello # Declare a Python extension called hello
: hello.cpp # source
&lt;dll&gt;../../build/boost_python # dependencies
;
]</p><p>
First, we need to specify our location in the boost project hierarchy.
It so happens that the tutorial example is located in <tt class="literal">/libs/python/example/tutorial</tt>.
Thus:</p><pre class="programlisting"><tt class="literal"> subproject libs/python/example/tutorial ;
</tt></pre><p>
Then we will include the definitions needed by Python modules:</p><pre class="programlisting"><tt class="literal"> SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;
</tt></pre><p>
Finally we declare our <tt class="literal">hello</tt> extension:</p><pre class="programlisting"><tt class="literal"> extension hello # Declare a Python extension called hello
: hello.cpp # source
&lt;dll&gt;../../build/boost_python # dependencies
;
</tt></pre><a name="hello.running_bjam"></a><h2><a name="id401888"></a>Running bjam</h2><p><span class="emphasis"><em>bjam</em></span> is run using your operating system's command line interpreter.</p><div class="blockquote"><blockquote class="blockquote"><p>Start it up.</p></blockquote></div><p>
Make sure that the environment is set so that we can invoke the C++
compiler. With MSVC, that would mean running the <tt class="literal">Vcvars32.bat</tt> batch
file. For instance:</p><pre class="programlisting"><tt class="literal"><span class="identifier"> C</span><span class="special">:\</span><span class="identifier">Program</span><span class="identifier"> Files</span><span class="special">\</span><span class="identifier">Microsoft</span><span class="identifier"> Visual</span><span class="identifier"> Studio</span><span class="special">\</span><span class="identifier">VC98</span><span class="special">\</span><span class="identifier">bin</span><span class="special">\</span><span class="identifier">Vcvars32</span><span class="special">.</span><span class="identifier">bat</span></tt></pre><p>
Some environment variables will have to be setup for proper building of our
Python modules. Example:</p><pre class="programlisting"><tt class="literal"><span class="identifier"> set</span><span class="identifier"> PYTHON_ROOT</span><span class="special">=</span><span class="identifier">c</span><span class="special">:/</span><span class="identifier">dev</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">python</span><span class="identifier">
set</span><span class="identifier"> PYTHON_VERSION</span><span class="special">=</span><span class="number">2.2</span></tt></pre><p>
The above assumes that the Python installation is in <tt class="literal">c:/dev/tools/python</tt>
and that we are using Python version 2.2. You'll have to tweak this path
appropriately. <span class="inlinemediaobject"><img src="images/note.gif"></span> Be sure not to include a third number, e.g. <span class="bold"><b>not</b></span> "2.2.1",
even if that's the version you have.</p><p>
Now we are ready... Be sure to <tt class="literal">cd</tt> to <tt class="literal">libs/python/example/tutorial</tt>
where the tutorial <tt class="literal">"hello.cpp"</tt> and the <tt class="literal">"Jamfile"</tt> is situated.</p><p>
Finally:</p><pre class="programlisting"><tt class="literal"><span class="identifier"> bjam</span><span class="special"> -</span><span class="identifier">sTOOLS</span><span class="special">=</span><span class="identifier">msvc</span></tt></pre><p>
We are again assuming that we are using Microsoft Visual C++ version 6. If
not, then you will have to specify the appropriate tool. See
<a href="../../../../../tools/build/index.html" target="_top">
Building Boost Libraries</a> for
further details.</p><p>
It should be building now:</p><pre class="programlisting"><tt class="literal"> cd C:\dev\boost\libs\python\example\tutorial
bjam -sTOOLS=msvc
...patience...
...found 1703 targets...
...updating 40 targets...
</tt></pre><p>
And so on... Finally:</p><pre class="programlisting"><tt class="literal"> vc-C++ ........\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
runtime-link-dynamic\hello.obj
hello.cpp
vc-Link ........\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
runtime-link-dynamic\hello.pyd ........\libs\python\example\tutorial\bin\
hello.pyd\msvc\debug\runtime-link-dynamic\hello.lib
Creating library ........\libs\python\example\tutorial\bin\hello.pyd\
msvc\debug\runtime-link-dynamic\hello.lib and object ........\libs\python\
example\tutorial\bin\hello.pyd\msvc\debug\runtime-link-dynamic\hello.exp
...updated 40 targets...
</tt></pre><p>
If all is well, you should now have:</p><div class="itemizedlist"><ul type="disc"><li>
boost_python.dll
</li><li>
hello.pyd
</li></ul></div><p>
if you are on Windows, and</p><div class="itemizedlist"><ul type="disc"><li>
libboost_python.so
</li><li>
hello.so
</li></ul></div><p>
if you are on Unix.</p><p><tt class="literal">boost_python.dll</tt> can be found somewhere in <tt class="literal">libs\python\build\bin</tt>
while <tt class="literal">hello.pyd</tt> can be found somewhere in
<tt class="literal">libs\python\example\tutorial\bin</tt>. After a successful build, you can just
link in these DLLs with the Python interpreter. In Windows for example, you
can simply put these libraries inside the directory where the Python
executable is.</p><p>
You may now fire up Python and run our hello module:</p><pre class="programlisting"><tt class="literal"><span class="special"> &gt;&gt;&gt;</span><span class="identifier"> import</span><span class="identifier"> hello</span><span class="special">
&gt;&gt;&gt;</span><span class="identifier"> print</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span><span class="identifier">
hello</span><span class="special">,</span><span class="identifier"> world</span></tt></pre><div class="blockquote"><blockquote class="blockquote"><p><span class="bold"><b>There you go... Have fun!</b></span></p></blockquote></div></div><table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr><td align="left"></td><td align="right"><small>Copyright © 2002-2004 Joel de Guzman, David Abrahams</small></td></tr></table><hr><div class="spirit-nav"><a accesskey="p" href="index.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="boost_python.exposing.html"><img src="images/next.png" alt="Next"></a></div></body></html>