~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/extending/building.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>3. Building C and C++ Extensions with distutils &mdash; Python 2.7.11 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.11',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 2.7.11 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
 
    <link rel="up" title="Extending and Embedding the Python Interpreter" href="index.html" />
34
 
    <link rel="next" title="4. Building C and C++ Extensions on Windows" href="windows.html" />
35
 
    <link rel="prev" title="2. Defining New Types" href="newtypes.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="windows.html" title="4. Building C and C++ Extensions on Windows"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="newtypes.html" title="2. Defining New Types"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.11</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Extending and Embedding the Python Interpreter</a> &raquo;</li> 
68
 
      </ul>
69
 
    </div>    
70
 
 
71
 
    <div class="document">
72
 
      <div class="documentwrapper">
73
 
        <div class="bodywrapper">
74
 
          <div class="body" role="main">
75
 
            
76
 
  <div class="section" id="building-c-and-c-extensions-with-distutils">
77
 
<span id="building"></span><h1>3. Building C and C++ Extensions with distutils<a class="headerlink" href="#building-c-and-c-extensions-with-distutils" title="Permalink to this headline">¶</a></h1>
78
 
<p>Starting in Python 1.4, Python provides, on Unix, a special make file for
79
 
building make files for building dynamically-linked extensions and custom
80
 
interpreters.  Starting with Python 2.0, this mechanism (known as related to
81
 
Makefile.pre.in, and Setup files) is no longer supported. Building custom
82
 
interpreters was rarely used, and extension modules can be built using
83
 
distutils.</p>
84
 
<p>Building an extension module using distutils requires that distutils is
85
 
installed on the build machine, which is included in Python 2.x and available
86
 
separately for Python 1.5. Since distutils also supports creation of binary
87
 
packages, users don&#8217;t necessarily need a compiler and distutils to install the
88
 
extension.</p>
89
 
<p>A distutils package contains a driver script, <code class="file docutils literal"><span class="pre">setup.py</span></code>. This is a plain
90
 
Python file, which, in the most simple case, could look like this:</p>
91
 
<div class="highlight-c"><div class="highlight"><pre>from distutils.core import setup, Extension
92
 
 
93
 
module1 = Extension(&#39;demo&#39;,
94
 
                    sources = [&#39;demo.c&#39;])
95
 
 
96
 
setup (name = &#39;PackageName&#39;,
97
 
       version = &#39;1.0&#39;,
98
 
       description = &#39;This is a demo package&#39;,
99
 
       ext_modules = [module1])
100
 
</pre></div>
101
 
</div>
102
 
<p>With this <code class="file docutils literal"><span class="pre">setup.py</span></code>, and a file <code class="file docutils literal"><span class="pre">demo.c</span></code>, running</p>
103
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">python</span> <span class="n">setup</span><span class="p">.</span><span class="n">py</span> <span class="n">build</span>
104
 
</pre></div>
105
 
</div>
106
 
<p>will compile <code class="file docutils literal"><span class="pre">demo.c</span></code>, and produce an extension module named <code class="docutils literal"><span class="pre">demo</span></code> in
107
 
the <code class="file docutils literal"><span class="pre">build</span></code> directory. Depending on the system, the module file will end
108
 
up in a subdirectory <code class="file docutils literal"><span class="pre">build/lib.system</span></code>, and may have a name like
109
 
<code class="file docutils literal"><span class="pre">demo.so</span></code> or <code class="file docutils literal"><span class="pre">demo.pyd</span></code>.</p>
110
 
<p>In the <code class="file docutils literal"><span class="pre">setup.py</span></code>, all execution is performed by calling the <code class="docutils literal"><span class="pre">setup</span></code>
111
 
function. This takes a variable number of keyword arguments, of which the
112
 
example above uses only a subset. Specifically, the example specifies
113
 
meta-information to build packages, and it specifies the contents of the
114
 
package.  Normally, a package will contain of addition modules, like Python
115
 
source modules, documentation, subpackages, etc. Please refer to the distutils
116
 
documentation in <a class="reference internal" href="../distutils/index.html#distutils-index"><span>Distributing Python Modules</span></a> to learn more about the features of
117
 
distutils; this section explains building extension modules only.</p>
118
 
<p>It is common to pre-compute arguments to <code class="xref py py-func docutils literal"><span class="pre">setup()</span></code>, to better structure the
119
 
driver script. In the example above, the <code class="docutils literal"><span class="pre">ext_modules</span></code> argument to
120
 
<code class="xref py py-func docutils literal"><span class="pre">setup()</span></code> is a list of extension modules, each of which is an instance of
121
 
the <code class="xref py py-class docutils literal"><span class="pre">Extension</span></code>. In the example, the instance
122
 
defines an extension named <code class="docutils literal"><span class="pre">demo</span></code> which is build by compiling a single source
123
 
file, <code class="file docutils literal"><span class="pre">demo.c</span></code>.</p>
124
 
<p>In many cases, building an extension is more complex, since additional
125
 
preprocessor defines and libraries may be needed. This is demonstrated in the
126
 
example below.</p>
127
 
<div class="highlight-c"><div class="highlight"><pre>from distutils.core import setup, Extension
128
 
 
129
 
module1 = Extension(&#39;demo&#39;,
130
 
                    define_macros = [(&#39;MAJOR_VERSION&#39;, &#39;1&#39;),
131
 
                                     (&#39;MINOR_VERSION&#39;, &#39;0&#39;)],
132
 
                    include_dirs = [&#39;/usr/local/include&#39;],
133
 
                    libraries = [&#39;tcl83&#39;],
134
 
                    library_dirs = [&#39;/usr/local/lib&#39;],
135
 
                    sources = [&#39;demo.c&#39;])
136
 
 
137
 
setup (name = &#39;PackageName&#39;,
138
 
       version = &#39;1.0&#39;,
139
 
       description = &#39;This is a demo package&#39;,
140
 
       author = &#39;Martin v. Loewis&#39;,
141
 
       author_email = &#39;martin@v.loewis.de&#39;,
142
 
       url = &#39;https://docs.python.org/extending/building&#39;,
143
 
       long_description = &#39;&#39;&#39;
144
 
This is really just a demo package.
145
 
&#39;&#39;&#39;,
146
 
       ext_modules = [module1])
147
 
</pre></div>
148
 
</div>
149
 
<p>In this example, <code class="xref py py-func docutils literal"><span class="pre">setup()</span></code> is called with additional meta-information, which
150
 
is recommended when distribution packages have to be built. For the extension
151
 
itself, it specifies preprocessor defines, include directories, library
152
 
directories, and libraries. Depending on the compiler, distutils passes this
153
 
information in different ways to the compiler. For example, on Unix, this may
154
 
result in the compilation commands</p>
155
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">gcc</span> <span class="o">-</span><span class="n">DNDEBUG</span> <span class="o">-</span><span class="n">g</span> <span class="o">-</span><span class="n">O3</span> <span class="o">-</span><span class="n">Wall</span> <span class="o">-</span><span class="n">Wstrict</span><span class="o">-</span><span class="n">prototypes</span> <span class="o">-</span><span class="n">fPIC</span> <span class="o">-</span><span class="n">DMAJOR_VERSION</span><span class="o">=</span><span class="mi">1</span> <span class="o">-</span><span class="n">DMINOR_VERSION</span><span class="o">=</span><span class="mi">0</span> <span class="o">-</span><span class="n">I</span><span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">include</span> <span class="o">-</span><span class="n">I</span><span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">include</span><span class="o">/</span><span class="n">python2</span><span class="mf">.2</span> <span class="o">-</span><span class="n">c</span> <span class="n">demo</span><span class="p">.</span><span class="n">c</span> <span class="o">-</span><span class="n">o</span> <span class="n">build</span><span class="o">/</span><span class="n">temp</span><span class="p">.</span><span class="n">linux</span><span class="o">-</span><span class="n">i686</span><span class="o">-</span><span class="mf">2.2</span><span class="o">/</span><span class="n">demo</span><span class="p">.</span><span class="n">o</span>
156
 
 
157
 
<span class="n">gcc</span> <span class="o">-</span><span class="n">shared</span> <span class="n">build</span><span class="o">/</span><span class="n">temp</span><span class="p">.</span><span class="n">linux</span><span class="o">-</span><span class="n">i686</span><span class="o">-</span><span class="mf">2.2</span><span class="o">/</span><span class="n">demo</span><span class="p">.</span><span class="n">o</span> <span class="o">-</span><span class="n">L</span><span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">lib</span> <span class="o">-</span><span class="n">ltcl83</span> <span class="o">-</span><span class="n">o</span> <span class="n">build</span><span class="o">/</span><span class="n">lib</span><span class="p">.</span><span class="n">linux</span><span class="o">-</span><span class="n">i686</span><span class="o">-</span><span class="mf">2.2</span><span class="o">/</span><span class="n">demo</span><span class="p">.</span><span class="n">so</span>
158
 
</pre></div>
159
 
</div>
160
 
<p>These lines are for demonstration purposes only; distutils users should trust
161
 
that distutils gets the invocations right.</p>
162
 
<div class="section" id="distributing-your-extension-modules">
163
 
<span id="distributing"></span><h2>3.1. Distributing your extension modules<a class="headerlink" href="#distributing-your-extension-modules" title="Permalink to this headline">¶</a></h2>
164
 
<p>When an extension has been successfully build, there are three ways to use it.</p>
165
 
<p>End-users will typically want to install the module, they do so by running</p>
166
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">python</span> <span class="n">setup</span><span class="p">.</span><span class="n">py</span> <span class="n">install</span>
167
 
</pre></div>
168
 
</div>
169
 
<p>Module maintainers should produce source packages; to do so, they run</p>
170
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">python</span> <span class="n">setup</span><span class="p">.</span><span class="n">py</span> <span class="n">sdist</span>
171
 
</pre></div>
172
 
</div>
173
 
<p>In some cases, additional files need to be included in a source distribution;
174
 
this is done through a <code class="file docutils literal"><span class="pre">MANIFEST.in</span></code> file; see the distutils documentation
175
 
for details.</p>
176
 
<p>If the source distribution has been build successfully, maintainers can also
177
 
create binary distributions. Depending on the platform, one of the following
178
 
commands can be used to do so.</p>
179
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">python</span> <span class="n">setup</span><span class="p">.</span><span class="n">py</span> <span class="n">bdist_wininst</span>
180
 
<span class="n">python</span> <span class="n">setup</span><span class="p">.</span><span class="n">py</span> <span class="n">bdist_rpm</span>
181
 
<span class="n">python</span> <span class="n">setup</span><span class="p">.</span><span class="n">py</span> <span class="n">bdist_dumb</span>
182
 
</pre></div>
183
 
</div>
184
 
</div>
185
 
</div>
186
 
 
187
 
 
188
 
          </div>
189
 
        </div>
190
 
      </div>
191
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
192
 
        <div class="sphinxsidebarwrapper">
193
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
194
 
  <ul>
195
 
<li><a class="reference internal" href="#">3. Building C and C++ Extensions with distutils</a><ul>
196
 
<li><a class="reference internal" href="#distributing-your-extension-modules">3.1. Distributing your extension modules</a></li>
197
 
</ul>
198
 
</li>
199
 
</ul>
200
 
 
201
 
  <h4>Previous topic</h4>
202
 
  <p class="topless"><a href="newtypes.html"
203
 
                        title="previous chapter">2. Defining New Types</a></p>
204
 
  <h4>Next topic</h4>
205
 
  <p class="topless"><a href="windows.html"
206
 
                        title="next chapter">4. Building C and C++ Extensions on Windows</a></p>
207
 
<h3>This Page</h3>
208
 
<ul class="this-page-menu">
209
 
  <li><a href="../bugs.html">Report a Bug</a></li>
210
 
  <li><a href="../_sources/extending/building.txt"
211
 
         rel="nofollow">Show Source</a></li>
212
 
</ul>
213
 
 
214
 
<div id="searchbox" style="display: none" role="search">
215
 
  <h3>Quick search</h3>
216
 
    <form class="search" action="../search.html" method="get">
217
 
      <input type="text" name="q" />
218
 
      <input type="submit" value="Go" />
219
 
      <input type="hidden" name="check_keywords" value="yes" />
220
 
      <input type="hidden" name="area" value="default" />
221
 
    </form>
222
 
    <p class="searchtip" style="font-size: 90%">
223
 
    Enter search terms or a module, class or function name.
224
 
    </p>
225
 
</div>
226
 
<script type="text/javascript">$('#searchbox').show(0);</script>
227
 
        </div>
228
 
      </div>
229
 
      <div class="clearer"></div>
230
 
    </div>  
231
 
    <div class="related" role="navigation" aria-label="related navigation">
232
 
      <h3>Navigation</h3>
233
 
      <ul>
234
 
        <li class="right" style="margin-right: 10px">
235
 
          <a href="../genindex.html" title="General Index"
236
 
             >index</a></li>
237
 
        <li class="right" >
238
 
          <a href="../py-modindex.html" title="Python Module Index"
239
 
             >modules</a> |</li>
240
 
        <li class="right" >
241
 
          <a href="windows.html" title="4. Building C and C++ Extensions on Windows"
242
 
             >next</a> |</li>
243
 
        <li class="right" >
244
 
          <a href="newtypes.html" title="2. Defining New Types"
245
 
             >previous</a> |</li>
246
 
        <li><img src="../_static/py.png" alt=""
247
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
248
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
249
 
        <li>
250
 
          <span class="version_switcher_placeholder">2.7.11</span>
251
 
          <a href="../index.html">Documentation</a> &raquo;
252
 
        </li>
253
 
 
254
 
          <li class="nav-item nav-item-1"><a href="index.html" >Extending and Embedding the Python Interpreter</a> &raquo;</li> 
255
 
      </ul>
256
 
    </div>  
257
 
    <div class="footer">
258
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
259
 
    <br />
260
 
    The Python Software Foundation is a non-profit corporation.
261
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
262
 
    <br />
263
 
    Last updated on Jan 23, 2016.
264
 
    <a href="../bugs.html">Found a bug</a>?
265
 
    <br />
266
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
267
 
    </div>
268
 
 
269
 
  </body>
270
 
</html>
 
 
b'\\ No newline at end of file'