perlxstut.html

来自「perl教程」· HTML 代码 · 共 907 行 · 第 1/5 页

HTML
907 行
字号
<p>Here we'll create some files that will generate a test library.  These will
include a C source file and a header file.  We'll also create a Makefile.PL
in this directory.  Then we'll make sure that running make at the Mytest2
level will automatically run this Makefile.PL file and the resulting Makefile.</p>
<p>In the mylib directory, create a file mylib.h that looks like this:</p>
<pre>
        <span class="comment">#define TESTVAL 4</span>
</pre>
<pre>
        <span class="variable">extern</span> <span class="variable">double</span>   <span class="variable">foo</span><span class="operator">(</span><span class="keyword">int</span><span class="operator">,</span> <span class="variable">long</span><span class="operator">,</span> <span class="variable">const</span> <span class="variable">char</span><span class="operator">*);</span>
</pre>
<p>Also create a file mylib.c that looks like this:</p>
<pre>
        <span class="comment">#include &lt;stdlib.h&gt;</span>
        <span class="comment">#include "./mylib.h"</span>
</pre>
<pre>
        <span class="variable">double</span>
        <span class="variable">foo</span><span class="operator">(</span><span class="keyword">int</span> <span class="variable">a</span><span class="operator">,</span> <span class="variable">long</span> <span class="variable">b</span><span class="operator">,</span> <span class="variable">const</span> <span class="variable">char</span> <span class="variable">*c</span><span class="operator">)</span>
        <span class="operator">{</span>
                <span class="keyword">return</span> <span class="operator">(</span><span class="variable">a</span> <span class="operator">+</span> <span class="variable">b</span> <span class="operator">+</span> <span class="variable">atof</span><span class="operator">(</span><span class="variable">c</span><span class="operator">)</span> <span class="operator">+</span> <span class="variable">TESTVAL</span><span class="operator">);</span>
        <span class="operator">}</span>
</pre>
<p>And finally create a file Makefile.PL that looks like this:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">ExtUtils::MakeMaker</span><span class="operator">;</span>
        <span class="variable">$Verbose</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
        <span class="variable">WriteMakefile</span><span class="operator">(</span>
            <span class="string">NAME</span>   <span class="operator">=&gt;</span> <span class="string">'Mytest2::mylib'</span><span class="operator">,</span>
            <span class="string">SKIP</span>   <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">qw(all static static_lib dynamic dynamic_lib)</span><span class="operator">]</span><span class="operator">,</span>
            <span class="string">clean</span>  <span class="operator">=&gt;</span> <span class="operator">{</span><span class="string">'FILES'</span> <span class="operator">=&gt;</span> <span class="string">'libmylib$(LIB_EXT)'</span><span class="operator">}</span><span class="operator">,</span>
        <span class="operator">);</span>
</pre>
<pre>
        sub MY::top_targets {
                '
        all :: static</pre>
<pre>
        pure_all :: static</pre>
<pre>
        static ::       libmylib$(LIB_EXT)</pre>
<pre>
        libmylib$(LIB_EXT): $(O_FILES)
                $(AR) cr libmylib$(LIB_EXT) $(O_FILES)
                $(RANLIB) libmylib$(LIB_EXT)</pre>
<pre>
        ';
        }</pre>
<p>Make sure you use a tab and not spaces on the lines beginning with &quot;$(AR)&quot;
and &quot;$(RANLIB)&quot;.  Make will not function properly if you use spaces.
It has also been reported that the &quot;cr&quot; argument to $(AR) is unnecessary
on Win32 systems.</p>
<p>We will now create the main top-level Mytest2 files.  Change to the directory
above Mytest2 and run the following command:</p>
<pre>
        % h2xs -O -n Mytest2 ./Mytest2/mylib/mylib.h</pre>
<p>This will print out a warning about overwriting Mytest2, but that's okay.
Our files are stored in Mytest2/mylib, and will be untouched.</p>
<p>The normal Makefile.PL that h2xs generates doesn't know about the mylib
directory.  We need to tell it that there is a subdirectory and that we
will be generating a library in it.  Let's add the argument MYEXTLIB to
the WriteMakefile call so that it looks like this:</p>
<pre>
        <span class="variable">WriteMakefile</span><span class="operator">(</span>
            <span class="string">'NAME'</span>      <span class="operator">=&gt;</span> <span class="string">'Mytest2'</span><span class="operator">,</span>
            <span class="string">'VERSION_FROM'</span> <span class="operator">=&gt;</span> <span class="string">'Mytest2.pm'</span><span class="operator">,</span> <span class="comment"># finds $VERSION</span>
            <span class="string">'LIBS'</span>      <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">''</span><span class="operator">]</span><span class="operator">,</span>   <span class="comment"># e.g., '-lm'</span>
            <span class="string">'DEFINE'</span>    <span class="operator">=&gt;</span> <span class="string">''</span><span class="operator">,</span>     <span class="comment"># e.g., '-DHAVE_SOMETHING'</span>
            <span class="string">'INC'</span>       <span class="operator">=&gt;</span> <span class="string">''</span><span class="operator">,</span>     <span class="comment"># e.g., '-I/usr/include/other'</span>
            <span class="string">'MYEXTLIB'</span> <span class="operator">=&gt;</span> <span class="string">'mylib/libmylib$(LIB_EXT)'</span><span class="operator">,</span>
        <span class="operator">);</span>
</pre>
<p>and then at the end add a subroutine (which will override the pre-existing
subroutine).  Remember to use a tab character to indent the line beginning
with &quot;cd&quot;!</p>
<pre>
        <span class="keyword">sub</span><span class="variable"> MY::postamble </span><span class="operator">{</span>
        <span class="string">'
        $(MYEXTLIB): mylib/Makefile
                cd mylib &amp;&amp; $(MAKE) $(PASSTHRU)
        '</span><span class="operator">;</span>
        <span class="operator">}</span>
</pre>
<p>Let's also fix the MANIFEST file so that it accurately reflects the contents
of our extension.  The single line that says &quot;mylib&quot; should be replaced by
the following three lines:</p>
<pre>
        mylib/Makefile.PL
        mylib/mylib.c
        mylib/mylib.h</pre>
<p>To keep our namespace nice and unpolluted, edit the .pm file and change
the variable <code>@EXPORT</code> to <code>@EXPORT_OK</code>.  Finally, in the
.xs file, edit the #include line to read:</p>
<pre>
        <span class="comment">#include "mylib/mylib.h"</span>
</pre>
<p>And also add the following function definition to the end of the .xs file:</p>
<pre>
        double
        foo(a,b,c)
                int             a
                long            b
                const char *    c
            OUTPUT:
                RETVAL</pre>
<p>Now we also need to create a typemap file because the default Perl doesn't
currently support the const char * type.  Create a file called typemap in
the Mytest2 directory and place the following in it:</p>
<pre>
        const char *    T_PV</pre>
<p>Now run perl on the top-level Makefile.PL.  Notice that it also created a
Makefile in the mylib directory.  Run make and watch that it does cd into
the mylib directory and run make in there as well.</p>
<p>Now edit the test.pl script and change the BEGIN block to print &quot;1..4&quot;,
and add the following lines to the end of the script:</p>
<pre>
        <span class="keyword">print</span> <span class="operator">&amp;</span><span class="variable">Mytest2::foo</span><span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="string">"Hello, world!"</span><span class="operator">)</span> <span class="operator">==</span> <span class="number">7</span> <span class="operator">?</span> <span class="string">"ok 2\n"</span> <span class="operator">:</span> <span class="string">"not ok 2\n"</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="operator">&amp;</span><span class="variable">Mytest2::foo</span><span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="string">"0.0"</span><span class="operator">)</span> <span class="operator">==</span> <span class="number">7</span> <span class="operator">?</span> <span class="string">"ok 3\n"</span> <span class="operator">:</span> <span class="string">"not ok 3\n"</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="keyword">abs</span><span class="operator">(&amp;</span><span class="variable">Mytest2::foo</span><span class="operator">(</span><span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"-3.4"</span><span class="operator">)</span> <span class="operator">-</span> <span class="number">0</span><span class="operator">.</span><span class="number">6</span><span class="operator">)</span> <span class="operator">&lt;=</span> <span class="number">0</span><span class="operator">.</span><span class="number">01</span> <span class="operator">?</span> <span class="string">"ok 4\n"</span> <span class="operator">:</span> <span class="string">"not ok 4\n"</span><span class="operator">;</span>
</pre>
<p>(When dealing with floating-point comparisons, it is best to not check for
equality, but rather that the difference between the expected and actual
result is below a certain amount (called epsilon) which is 0.01 in this case)</p>
<p>Run &quot;<code>make test</code>&quot; and all should be well.</p>
<p>
</p>
<h2><a name="what_has_happened_here">What has happened here?</a></h2>
<p>Unlike previous examples, we've now run h2xs on a real include file.  This
has caused some extra goodies to appear in both the .pm and .xs files.</p>
<ul>
<li>
<p>In the .xs file, there's now a #include directive with the absolute path to
the mylib.h header file.  We changed this to a relative path so that we
could move the extension directory if we wanted to.</p>
</li>
<li>
<p>There's now some new C code that's been added to the .xs file.  The purpose
of the <code>constant</code> routine is to make the values that are #define'd in the
header file accessible by the Perl script (by calling either <code>TESTVAL</code> or
<code>&amp;Mytest2::TESTVAL</code>).  There's also some XS code to allow calls to the
<code>constant</code> routine.</p>
</li>
<li>
<p>The .pm file originally exported the name <code>TESTVAL</code> in the <code>@EXPORT</code> array.
This could lead to name clashes.  A good rule of thumb is that if the #define
is only going to be used by the C routines themselves, and not by the user,
they should be removed from the <code>@EXPORT</code> array.  Alternately, if you don't
mind using the &quot;fully qualified name&quot; of a variable, you could move most
or all of the items from the <code>@EXPORT</code> array into the <code>@EXPORT_OK</code> array.</p>
</li>
<li>
<p>If our include file had contained #include directives, these would not have
been processed by h2xs.  There is no good solution to this right now.</p>
</li>
<li>
<p>We've also told Perl about the library that we built in the mylib
subdirectory.  That required only the addition of the <code>MYEXTLIB</code> variable
to the WriteMakefile call and the replacement of the postamble subroutine
to cd into the subdirectory and run make.  The Makefile.PL for the
library is a bit more complicated, but not excessively so.  Again we
replaced the postamble subroutine to insert our own code.  This code
simply specified that the library to be created here was a static archive
library (as opposed to a dynamically loadable library) and provided the
commands to build it.</p>
</li>
</ul>
<p>
</p>
<h2><a name="anatomy_of__xs_file">Anatomy of .xs file</a></h2>
<p>The .xs file of <a href="#example_4">EXAMPLE 4</a> contained some new elements.  To understand
the meaning of these elements, pay attention to the line which reads</p>
<pre>
        MODULE = Mytest2                PACKAGE = Mytest2</pre>
<p>Anything before this line is plain C code which describes which headers
to include, and defines some convenience functions.  No translations are
performed on this part, apart from having embedded POD documentation
skipped over (see <a href="../../lib/Pod/perlpod.html">the perlpod manpage</a>) it goes into the generated output C file as is.</p>
<p>Anything after this line is the description of XSUB functions.
These descriptions are translated by <strong>xsubpp</strong> into C code which
implements these functions using Perl calling conventions, and which
makes these functions visible from Perl interpreter.</p>
<p>Pay a special attention to the function <code>constant</code>.  This name appears

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?