Skip to content

Commit a295ac6

Browse files
committed
Tutorials...
[SVN r15818]
1 parent e49e0d2 commit a295ac6

File tree

4 files changed

+302
-6
lines changed

4 files changed

+302
-6
lines changed

doc/tutorial/doc/building_hello_world.html

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,131 @@
2828
Now the first thing you'd want to do is to build the Hello World module and
2929
try it for yourself in Python. In this section, we shall outline the steps
3030
necessary to achieve that. We shall use the build tool that comes bundled
31-
with every boost distribution: <b>bjam</b>. For a complete reference to building
32-
Boost.Python, check out: <a href="../../building.html">
33-
building.html</a></p>
34-
<table border="0">
31+
with every boost distribution: <b>bjam</b>.</p>
32+
<p>
33+
We shall skip over the details. Our objective will be to simply create the
34+
hello world module and run it in Python. For a complete reference to
35+
building Boost.Python, check out: <a href="../../building.html">
36+
building.html</a>.
37+
After this brief <i>bjam</i> tutorial, we should have built two DLLs:</p>
38+
<ul><li>boost_python.dll</li><li>hello.pyd</li></ul><p>
39+
This assumes of course that we are running on Windows.</p>
40+
<p>
41+
The tutorial example can be found in the directory:
42+
<tt>/libs/python/example/tutorial</tt>. There, you can find:</p>
43+
<ul><li>hello.cpp</li><li>Jamfile</li></ul><p>
44+
The <tt>hello.cpp</tt> file is our C++ hello world example. The <tt>Jamfile</tt> is a
45+
minimalist <i>bjam</i> script that builds the DLLs for us.</p>
46+
<p>
47+
Before anything else, you should have the bjam executable in your boost
48+
directory. Pre-built Boost.Jam executables are available for some
49+
platforms. For example, a pre-built Microsoft Windows bjam executable can
50+
be downloaded <a href="http://boost.sourceforge.net/jam-executables/bin.ntx86/bjam.zip">
51+
here</a>.
52+
The complete list of bjam pre-built executables can be found <a href="../../../../../tools/build/index.html#Jam">
53+
here</a>.</p>
54+
<a name="lets_jam_"></a><h2>Lets Jam!</h2><p>
55+
Here is our minimalist Jamfile:</p>
56+
<code><pre>
57+
subproject libs/python/example/tutorial ;
58+
59+
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
60+
include python.jam ;
61+
62+
extension hello # Declare a Python extension called hello
63+
: hello.cpp # source
64+
&lt;dll&gt;../../build/boost_python # dependencies
65+
;
66+
</pre></code><p>
67+
First, we need to specify our location in the boost project hierarchy.
68+
It so happens that the tutorial example is located in <tt>/libs/python/example/tutorial</tt>.
69+
Thus:</p>
70+
<code><pre>
71+
subproject libs/python/example/tutorial ;
72+
</pre></code><p>
73+
Then we will include the definitions needed by Python modules:</p>
74+
<code><pre>
75+
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
76+
include python.jam ;
77+
</pre></code><p>
78+
Finally we declare our <tt>hello</tt> extension:</p>
79+
<code><pre>
80+
extension hello # Declare a Python extension called hello
81+
: hello.cpp # source
82+
&lt;dll&gt;../../build/boost_python # dependencies
83+
;
84+
</pre></code><a name="running_bjam"></a><h2>Running bjam</h2><p>
85+
<i>bjam</i> is run using your operating system's command line interpreter.</p>
86+
<blockquote><p>Start it up.</p></blockquote><p>
87+
Make sure that the environment is set so that we can invoke the C++
88+
compiler. With MSVC, that would mean running the <tt>Vcvars32.bat</tt> batch
89+
file. For instance:</p>
90+
<code><pre>
91+
<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
92+
</span></pre></code>
93+
<p>
94+
Some environment variables will have to be setup for proper building of our
95+
Python modules. Example:</p>
96+
<code><pre>
97+
<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
98+
</span><span class=identifier>set </span><span class=identifier>PYTHON_VERSION</span><span class=special>=</span><span class=number>2.2
99+
</span></pre></code>
100+
<p>
101+
The above assumes that the Python installation is in <tt>c:/dev/tools/python</tt>
102+
and that we are using Python version 2.2. Be sure not to include a third
103+
number, e.g. <b>not</b> &quot;2.2.1&quot;, even if that's the version you have.</p>
104+
<p>
105+
Now we are ready... Be sure to <tt>cd</tt> to <tt>libs/python/example/tutorial</tt>
106+
where the tutorial <tt>&quot;hello.cpp&quot;</tt> and the <tt>&quot;Jamfile&quot;</tt> is situated.</p>
107+
<p>
108+
Finally:</p>
109+
<code><pre>
110+
<span class=identifier>bjam </span><span class=special>-</span><span class=identifier>sTOOLS</span><span class=special>=</span><span class=identifier>msvc
111+
</span></pre></code>
112+
<p>
113+
We are again assuming that we are using Microsoft Visual C++ version 6. If
114+
not, then you will have to specify the appropriate tool. See
115+
<a href="../../../../../tools/build/index.html">
116+
Building Boost Libraries</a> for
117+
further details.</p>
118+
<p>
119+
It should be building now:</p>
120+
<code><pre>
121+
cd C:\dev\boost\libs\python\example\tutorial
122+
bjam -sTOOLS=msvc
123+
...patience...
124+
...found 1703 targets...
125+
...updating 40 targets...
126+
</pre></code><p>
127+
And so on... Finally:</p>
128+
<code><pre>
129+
vc-C++ ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
130+
runtime-link-dynamic\hello.obj
131+
hello.cpp
132+
vc-Link ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
133+
runtime-link-dynamic\hello.pyd ..\..\..\..\libs\python\example\tutorial\bin\
134+
hello.pyd\msvc\debug\runtime-link-dynamic\hello.lib
135+
Creating library ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\
136+
msvc\debug\runtime-link-dynamic\hello.lib and object ..\..\..\..\libs\python\
137+
example\tutorial\bin\hello.pyd\msvc\debug\runtime-link-dynamic\hello.exp
138+
...updated 40 targets...
139+
</pre></code><p>
140+
If all is well, you should now have:</p>
141+
<ul><li>boost_python.dll</li><li>hello.pyd</li></ul><p>
142+
<tt>boost_python.dll</tt> can be found somewhere in <tt>libs\python\build\bin</tt>
143+
while <tt>hello.pyd</tt> can be found somewhere in
144+
<tt>libs\python\example\tutorial\bin</tt>. After a successful build, you can just
145+
link in these DLLs with the Python interpreter. In Windows for example, you
146+
can simply put these libraries inside the directory where the Python
147+
executable is.</p>
148+
<p>
149+
You may now fire up Python and run our hello module:</p>
150+
<code><pre>
151+
<span class=special>&gt;&gt;&gt; </span><span class=identifier>import </span><span class=identifier>hello
152+
</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>()
153+
</span><span class=identifier>hello</span><span class=special>, </span><span class=identifier>world
154+
</span></pre></code>
155+
<blockquote><p><b>There you go... Have fun!</b></p></blockquote><table border="0">
35156
<tr>
36157
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
37158
<td width="30"><a href="quickstart.html"><img src="theme/l_arr.gif" border="0"></a></td>

doc/tutorial/doc/quickstart.txt

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,151 @@ resulting DLL is now visible to Python. Here's a sample Python session:
5555
Now the first thing you'd want to do is to build the Hello World module and
5656
try it for yourself in Python. In this section, we shall outline the steps
5757
necessary to achieve that. We shall use the build tool that comes bundled
58-
with every boost distribution: [*bjam]. For a complete reference to building
59-
Boost.Python, check out: [@../../building.html building.html]
58+
with every boost distribution: [*bjam].
59+
60+
We shall skip over the details. Our objective will be to simply create the
61+
hello world module and run it in Python. For a complete reference to
62+
building Boost.Python, check out: [@../../building.html building.html].
63+
After this brief ['bjam] tutorial, we should have built two DLLs:
64+
65+
* boost_python.dll
66+
* hello.pyd
67+
68+
This assumes of course that we are running on Windows.
69+
70+
The tutorial example can be found in the directory:
71+
[^/libs/python/example/tutorial]. There, you can find:
72+
73+
* hello.cpp
74+
* Jamfile
75+
76+
The [^hello.cpp] file is our C++ hello world example. The [^Jamfile] is a
77+
minimalist ['bjam] script that builds the DLLs for us.
78+
79+
Before anything else, you should have the bjam executable in your boost
80+
directory. Pre-built Boost.Jam executables are available for some
81+
platforms. For example, a pre-built Microsoft Windows bjam executable can
82+
be downloaded [@http://boost.sourceforge.net/jam-executables/bin.ntx86/bjam.zip here].
83+
The complete list of bjam pre-built executables can be found [@../../../../../tools/build/index.html#Jam here].
84+
85+
[h2 Lets Jam!]
86+
87+
Here is our minimalist Jamfile:
88+
89+
[pre
90+
subproject libs/python/example/tutorial ;
91+
92+
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
93+
include python.jam ;
94+
95+
extension hello # Declare a Python extension called hello
96+
: hello.cpp # source
97+
<dll>../../build/boost_python # dependencies
98+
;
99+
]
100+
101+
First, we need to specify our location in the boost project hierarchy.
102+
It so happens that the tutorial example is located in [^/libs/python/example/tutorial].
103+
Thus:
104+
105+
[pre
106+
subproject libs/python/example/tutorial ;
107+
]
108+
109+
Then we will include the definitions needed by Python modules:
110+
111+
[pre
112+
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
113+
include python.jam ;
114+
]
115+
116+
Finally we declare our [^hello] extension:
117+
118+
[pre
119+
extension hello # Declare a Python extension called hello
120+
: hello.cpp # source
121+
<dll>../../build/boost_python # dependencies
122+
;
123+
]
124+
125+
[h2 Running bjam]
126+
127+
['bjam] is run using your operating system's command line interpreter.
128+
129+
[:Start it up.]
130+
131+
Make sure that the environment is set so that we can invoke the C++
132+
compiler. With MSVC, that would mean running the [^Vcvars32.bat] batch
133+
file. For instance:
134+
135+
C:\Program Files\Microsoft Visual Studio\VC98\bin\Vcvars32.bat
136+
137+
Some environment variables will have to be setup for proper building of our
138+
Python modules. Example:
139+
140+
set PYTHON_ROOT=c:/dev/tools/python
141+
set PYTHON_VERSION=2.2
142+
143+
The above assumes that the Python installation is in [^c:/dev/tools/python]
144+
and that we are using Python version 2.2. Be sure not to include a third
145+
number, e.g. [*not] "2.2.1", even if that's the version you have.
146+
147+
Now we are ready... Be sure to [^cd] to [^libs/python/example/tutorial]
148+
where the tutorial [^"hello.cpp"] and the [^"Jamfile"] is situated.
149+
150+
Finally:
151+
152+
bjam -sTOOLS=msvc
153+
154+
We are again assuming that we are using Microsoft Visual C++ version 6. If
155+
not, then you will have to specify the appropriate tool. See
156+
[@../../../../../tools/build/index.html Building Boost Libraries] for
157+
further details.
158+
159+
It should be building now:
160+
161+
[pre
162+
cd C:\dev\boost\libs\python\example\tutorial
163+
bjam -sTOOLS=msvc
164+
...patience...
165+
...found 1703 targets...
166+
...updating 40 targets...
167+
]
168+
169+
And so on... Finally:
170+
171+
[pre
172+
vc-C++ ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
173+
runtime-link-dynamic\hello.obj
174+
hello.cpp
175+
vc-Link ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
176+
runtime-link-dynamic\hello.pyd ..\..\..\..\libs\python\example\tutorial\bin\
177+
hello.pyd\msvc\debug\runtime-link-dynamic\hello.lib
178+
Creating library ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\
179+
msvc\debug\runtime-link-dynamic\hello.lib and object ..\..\..\..\libs\python\
180+
example\tutorial\bin\hello.pyd\msvc\debug\runtime-link-dynamic\hello.exp
181+
...updated 40 targets...
182+
]
183+
184+
If all is well, you should now have:
185+
186+
* boost_python.dll
187+
* hello.pyd
188+
189+
[^boost_python.dll] can be found somewhere in [^libs\python\build\bin]
190+
while [^hello.pyd] can be found somewhere in
191+
[^libs\python\example\tutorial\bin]. After a successful build, you can just
192+
link in these DLLs with the Python interpreter. In Windows for example, you
193+
can simply put these libraries inside the directory where the Python
194+
executable is.
195+
196+
You may now fire up Python and run our hello module:
197+
198+
>>> import hello
199+
>>> print hello.greet()
200+
hello, world
201+
202+
[:[*There you go... Have fun!]]
60203

61204
[page Exposing Classes]
62205

example/tutorial/Jamfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Hello World Example from the tutorial
2+
# [Joel de Guzman 10/9/2002]
3+
4+
# Specify our location in the boost project hierarchy
5+
subproject libs/python/example/tutorial ;
6+
7+
# Include definitions needed for Python modules
8+
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
9+
include python.jam ;
10+
11+
extension hello # Declare a Python extension called hello
12+
: hello.cpp # source
13+
<dll>../../build/boost_python # dependencies
14+
;
15+

example/tutorial/hello.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Hello World Example from the tutorial
2+
// [Joel de Guzman 10/9/2002]
3+
4+
char const* greet()
5+
{
6+
return "hello, world";
7+
}
8+
9+
#include <boost/python/module.hpp>
10+
#include <boost/python/def.hpp>
11+
using namespace boost::python;
12+
13+
BOOST_PYTHON_MODULE(hello)
14+
{
15+
def("greet", greet);
16+
}
17+

0 commit comments

Comments
 (0)