|
28 | 28 | Now the first thing you'd want to do is to build the Hello World module and |
29 | 29 | try it for yourself in Python. In this section, we shall outline the steps |
30 | 30 | 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 | + <dll>../../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 | + <dll>../../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> "2.2.1", 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>"hello.cpp"</tt> and the <tt>"Jamfile"</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>>>> </span><span class=identifier>import </span><span class=identifier>hello |
| 152 | + </span><span class=special>>>> </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"> |
35 | 156 | <tr> |
36 | 157 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
37 | 158 | <td width="30"><a href="quickstart.html"><img src="theme/l_arr.gif" border="0"></a></td> |
|
0 commit comments