1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>h2xs - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-gb" />
<link href="css.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen" />
</head>
<script language="JavaScript" type="text/javascript" src="label.js"></script>
<script language="JavaScript">
pageDepth = 0;
setPath();
</script>
<body onLoad="showToolbars();loadLabels()">
<div id="pageHeader">
<div id="pageHeaderLogo">
<img src="onion.gif">
</div>
<div id="pageHeaderText">
<a href="http://perldoc.perl.org">perldoc.perl.org</a>
</div>
</div>
<div id="pageBody">
<div id="left">
<div id="leftContent">
<div id="leftClose">
<a href="#" onClick="closeLeft()" title="Hide navigation" onmouseover="leftCloseIcon.src='close_purple.gif';" onmouseout="leftCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="leftCloseIcon" id="leftCloseIcon" border=0></a>
</div>
<h1>Manual:</h1>
<ul>
<li><a href="index-overview.html">Overview</a></li>
<li><a href="index-tutorials.html">Tutorials</a></li>
<li><a href="index-faq.html">FAQs</a></li>
<li><a href="index-history.html">History / Changes</a></li>
<li><a href="index-licence.html">Licence</a></li>
</ul>
<h2>Reference:</h2>
<ul>
<li><a href="index-language.html">Language</a></li>
<li><a href="index-functions.html">Functions</a></li>
<li><a href="perlop.html">Operators</a></li>
<li><a href="perlvar.html">Special variables</a></li>
<li><a href="index-pragmas.html">Pragmas</a></li>
<li><a href="index-modules-A.html">Core modules</a></li>
<li><a href="index-utilities.html">Utilities</a></li>
<li><a href="index-internals.html">Internals</a></li>
<li><a href="index-platforms.html">Platform specific</a></li>
</ul>
<h2>Links:</h2>
<ul>
<li><a href="http://search.cpan.org">CPAN</a></li>
<li><a href="http://www.perl.org">Perl.org</a></li>
<li><a href="http://www.perl.com">Perl.com</a></li>
<li><a href="http://perlbuzz.com">Perl Buzz</a></li>
<li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
<li><a href="http://jobs.perl.org">Perl Jobs</a></li>
<li><a href="http://www.pm.org">Perl Mongers</a></li>
<li><a href="http://www.perlmonks.org">Perl Monks</a></li>
<li><a href="http://planet.perl.org">Planet Perl</a></li>
<li><a href="http://use.perl.org">Use Perl</a></li>
</ul>
<h2>Contact:</h2>
<ul>
<li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
(<a href="http://perl.jonallen.info">JJ</a>)</li>
<li class="spaced">Last updated on<br>23 December 2007</li>
<li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
more details</li>
</ul>
</div>
</div>
<div id="center">
<div id="centerContent">
<div id="contentHeader">
<div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
<div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
<div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
</div>
<div id="breadCrumbs"><a href="index.html">Home</a> > <a href="index-utilities.html">Utilities</a> > h2xs</div>
<script language="JavaScript">fromSearch();</script>
<div id="contentBody"><div class="title_container"><div class="page_title">h2xs</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#OPTIONS">OPTIONS</a><li><a href="#EXAMPLES">EXAMPLES</a><ul><li><a href="#Extension-based-on-_.h_-and-_.c_-files">Extension based on <i>.h</i> and <i>.c</i> files</a></ul><li><a href="#ENVIRONMENT">ENVIRONMENT</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#DIAGNOSTICS">DIAGNOSTICS</a><li><a href="#LIMITATIONS-of-*-x*">LIMITATIONS of <b>-x</b></a></ul><a name="NAME"></a><h1>NAME</h1>
<p>h2xs - convert .h C header files to Perl extensions</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<p><b>h2xs</b> [<b>OPTIONS</b> ...] [headerfile ... [extra_libraries]]</p>
<p><b>h2xs</b> <b>-h</b>|<b>-?</b>|<b>--help</b></p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p><i>h2xs</i> builds a Perl extension from C header files. The extension
will include functions which can be used to retrieve the value of any
#define statement which was in the C header files.</p>
<p>The <i>module_name</i> will be used for the name of the extension. If
module_name is not supplied then the name of the first header file
will be used, with the first character capitalized.</p>
<p>If the extension might need extra libraries, they should be included
here. The extension Makefile.PL will take care of checking whether
the libraries actually exist and how they should be loaded. The extra
libraries should be specified in the form -lm -lposix, etc, just as on
the cc command line. By default, the Makefile.PL will search through
the library path determined by Configure. That path can be augmented
by including arguments of the form <b>-L/another/library/path</b> in the
extra-libraries argument.</p>
<p>In spite of its name, <i>h2xs</i> may also be used to create a skeleton pure
Perl module. See the <b>-X</b> option.</p>
<a name="OPTIONS"></a><h1>OPTIONS</h1>
<ul>
<li><a name="*-A*%2c-*--omit-autoload*"></a><b><b>-A</b>, <b>--omit-autoload</b></b>
<p>Omit all autoload facilities. This is the same as <b>-c</b> but also
removes the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">AutoLoader</span></code> statement from the .pm file.</p>
</li>
<li><a name="*-B*%2c-*--beta-version*"></a><b><b>-B</b>, <b>--beta-version</b></b>
<p>Use an alpha/beta style version number. Causes version number to
be "0.00_01" unless <b>-v</b> is specified.</p>
</li>
<li><a name="*-C*%2c-*--omit-changes*"></a><b><b>-C</b>, <b>--omit-changes</b></b>
<p>Omits creation of the <i>Changes</i> file, and adds a HISTORY section to
the POD template.</p>
</li>
<li><a name="*-F*%2c-*--cpp-flags*%3d_addflags_"></a><b><b>-F</b>, <b>--cpp-flags</b>=<i>addflags</i></b>
<p>Additional flags to specify to C preprocessor when scanning header for
function declarations. Writes these options in the generated <i>Makefile.PL</i>
too.</p>
</li>
<li><a name="*-M*%2c-*--func-mask*%3d_regular-expression_"></a><b><b>-M</b>, <b>--func-mask</b>=<i>regular expression</i></b>
<p>selects functions/macros to process.</p>
</li>
<li><a name="*-O*%2c-*--overwrite-ok*"></a><b><b>-O</b>, <b>--overwrite-ok</b></b>
<p>Allows a pre-existing extension directory to be overwritten.</p>
</li>
<li><a name="*-P*%2c-*--omit-pod*"></a><b><b>-P</b>, <b>--omit-pod</b></b>
<p>Omit the autogenerated stub POD section.</p>
</li>
<li><a name="*-X*%2c-*--omit-XS*"></a><b><b>-X</b>, <b>--omit-XS</b></b>
<p>Omit the XS portion. Used to generate a skeleton pure Perl module.
<code class="inline">-c</code>
and <code class="inline">-f</code>
are implicitly enabled.</p>
</li>
<li><a name="*-a*%2c-*--gen-accessors*"></a><b><b>-a</b>, <b>--gen-accessors</b></b>
<p>Generate an accessor method for each element of structs and unions. The
generated methods are named after the element name; will return the current
value of the element if called without additional arguments; and will set
the element to the supplied value (and return the new value) if called with
an additional argument. Embedded structures and unions are returned as a
pointer rather than the complete structure, to facilitate chained calls.</p>
<p>These methods all apply to the Ptr type for the structure; additionally
two methods are constructed for the structure type itself, <code class="inline"><span class="w">_to_ptr</span></code>
which returns a Ptr type pointing to the same structure, and a <code class="inline"><span class="w">new</span></code>
method to construct and return a new structure, initialised to zeroes.</p>
</li>
<li><a name="*-b*%2c-*--compat-version*%3d_version_"></a><b><b>-b</b>, <b>--compat-version</b>=<i>version</i></b>
<p>Generates a .pm file which is backwards compatible with the specified
perl version.</p>
<p>For versions < 5.6.0, the changes are.
- no use of 'our' (uses 'use vars' instead)
- no 'use warnings'</p>
<p>Specifying a compatibility version higher than the version of perl you
are using to run h2xs will have no effect. If unspecified h2xs will default
to compatibility with the version of perl you are using to run h2xs.</p>
</li>
<li><a name="*-c*%2c-*--omit-constant*"></a><b><b>-c</b>, <b>--omit-constant</b></b>
<p>Omit <code class="inline"><span class="i">constant</span><span class="s">(</span><span class="s">)</span></code>
from the .xs file and corresponding specialised
<code class="inline">AUTOLOAD</code>
from the .pm file.</p>
</li>
<li><a name="*-d*%2c-*--debugging*"></a><b><b>-d</b>, <b>--debugging</b></b>
<p>Turn on debugging messages.</p>
</li>
<li><a name="*-e*%2c-*--omit-enums*%3d%5b_regular-expression_%5d"></a><b><b>-e</b>, <b>--omit-enums</b>=[<i>regular expression</i>]</b>
<p>If <i>regular expression</i> is not given, skip all constants that are defined in
a C enumeration. Otherwise skip only those constants that are defined in an
enum whose name matches <i>regular expression</i>.</p>
<p>Since <i>regular expression</i> is optional, make sure that this switch is followed
by at least one other switch if you omit <i>regular expression</i> and have some
pending arguments such as header-file names. This is ok:</p>
<pre class="verbatim"> <span class="w">h2xs</span> -e -<span class="w">n</span> <span class="w">Module::Foo</span> <span class="w">foo</span>.<span class="w">h</span></pre>
<p>This is not ok:</p>
<pre class="verbatim"> <span class="w">h2xs</span> -<span class="w">n</span> <span class="w">Module::Foo</span> -e <span class="w">foo</span>.<span class="w">h</span></pre>
<p>In the latter, foo.h is taken as <i>regular expression</i>.</p>
</li>
<li><a name="*-f*%2c-*--force*"></a><b><b>-f</b>, <b>--force</b></b>
<p>Allows an extension to be created for a header even if that header is
not found in standard include directories.</p>
</li>
<li><a name="*-g*%2c-*--global*"></a><b><b>-g</b>, <b>--global</b></b>
<p>Include code for safely storing static data in the .xs file.
Extensions that do no make use of static data can ignore this option.</p>
</li>
<li><a name="*-h*%2c-*-%3f*%2c-*--help*"></a><b><b>-h</b>, <b>-?</b>, <b>--help</b></b>
<p>Print the usage, help and version for this h2xs and exit.</p>
</li>
<li><a name="*-k*%2c-*--omit-const-func*"></a><b><b>-k</b>, <b>--omit-const-func</b></b>
<p>For function arguments declared as <code class="inline"><span class="w">const</span></code>
, omit the const attribute in the
generated XS code.</p>
</li>
<li><a name="*-m*%2c-*--gen-tied-var*"></a><b><b>-m</b>, <b>--gen-tied-var</b></b>
<p><b>Experimental</b>: for each variable declared in the header file(s), declare
a perl variable of the same name magically tied to the C variable.</p>
</li>
<li><a name="*-n*%2c-*--name*%3d_module_name_"></a><b><b>-n</b>, <b>--name</b>=<i>module_name</i></b>
<p>Specifies a name to be used for the extension, e.g., -n RPC::DCE</p>
</li>
<li><a name="*-o*%2c-*--opaque-re*%3d_regular-expression_"></a><b><b>-o</b>, <b>--opaque-re</b>=<i>regular expression</i></b>
<p>Use "opaque" data type for the C types matched by the regular
expression, even if these types are <code class="inline"><span class="w">typedef</span></code>
-equivalent to types
from typemaps. Should not be used without <b>-x</b>.</p>
<p>This may be useful since, say, types which are <code class="inline"><span class="w">typedef</span></code>
-equivalent
to integers may represent OS-related handles, and one may want to work
with these handles in OO-way, as in <code class="inline"><span class="i">$handle</span><span class="i">->do_something</span><span class="s">(</span><span class="s">)</span></code>
.
Use <code class="inline">-o .</code>
if you want to handle all the <code class="inline"><span class="w">typedef</span></code>
ed types as opaque
types.</p>
<p>The type-to-match is whitewashed (except for commas, which have no
whitespace before them, and multiple <code class="inline"><span class="i">*</span></code>
which have no whitespace
between them).</p>
</li>
<li><a name="*-p*%2c-*--remove-prefix*%3d_prefix_"></a><b><b>-p</b>, <b>--remove-prefix</b>=<i>prefix</i></b>
<p>Specify a prefix which should be removed from the Perl function names,
e.g., -p sec_rgy_ This sets up the XS <b>PREFIX</b> keyword and removes
the prefix from functions that are autoloaded via the <code class="inline"><span class="i">constant</span><span class="s">(</span><span class="s">)</span></code>
mechanism.</p>
</li>
<li><a name="*-s*%2c-*--const-subs*%3d_sub1%2csub2_"></a><b><b>-s</b>, <b>--const-subs</b>=<i>sub1,sub2</i></b>
<p>Create a perl subroutine for the specified macros rather than autoload
with the constant() subroutine. These macros are assumed to have a
return type of <b>char *</b>, e.g.,
-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid.</p>
</li>
<li><a name="*-t*%2c-*--default-type*%3d_type_"></a><b><b>-t</b>, <b>--default-type</b>=<i>type</i></b>
<p>Specify the internal type that the constant() mechanism uses for macros.
The default is IV (signed integer). Currently all macros found during the
header scanning process will be assumed to have this type. Future versions
of <code class="inline"><span class="w">h2xs</span></code>
may gain the ability to make educated guesses.</p>
</li>
<li><a name="*--use-new-tests*"></a><b><b>--use-new-tests</b></b>
<p>When <b>--compat-version</b> (<b>-b</b>) is present the generated tests will use
<code class="inline"><span class="w">Test::More</span></code>
rather than <code class="inline"><span class="w">Test</span></code>
which is the default for versions before
5.7.2 . <code class="inline"><span class="w">Test::More</span></code>
will be added to PREREQ_PM in the generated
<code class="inline"><span class="w">Makefile</span>.<span class="w">PL</span></code>
.</p>
</li>
<li><a name="*--use-old-tests*"></a><b><b>--use-old-tests</b></b>
<p>Will force the generation of test code that uses the older <code class="inline"><span class="w">Test</span></code>
module.</p>
</li>
<li><a name="*--skip-exporter*"></a><b><b>--skip-exporter</b></b>
<p>Do not use <code class="inline"><span class="w">Exporter</span></code>
and/or export any symbol.</p>
</li>
<li><a name="*--skip-ppport*"></a><b><b>--skip-ppport</b></b>
<p>Do not use <code class="inline"><span class="w">Devel::PPPort</span></code>
: no portability to older version.</p>
</li>
<li><a name="*--skip-autoloader*"></a><b><b>--skip-autoloader</b></b>
<p>Do not use the module <code class="inline"><span class="w">AutoLoader</span></code>
; but keep the constant() function
and <code class="inline"><a name="AUTOLOAD"></a>sub <span class="m">AUTOLOAD</span></code>
for constants.</p>
</li>
<li><a name="*--skip-strict*"></a><b><b>--skip-strict</b></b>
<p>Do not use the pragma <code class="inline"><span class="w">strict</span></code>
.</p>
</li>
<li><a name="*--skip-warnings*"></a><b><b>--skip-warnings</b></b>
<p>Do not use the pragma <code class="inline"><span class="w">warnings</span></code>
.</p>
</li>
<li><a name="*-v*%2c-*--version*%3d_version_"></a><b><b>-v</b>, <b>--version</b>=<i>version</i></b>
<p>Specify a version number for this extension. This version number is added
to the templates. The default is 0.01, or 0.00_01 if <code class="inline">-B</code>
is specified.
The version specified should be numeric.</p>
</li>
<li><a name="*-x*%2c-*--autogen-xsubs*"></a><b><b>-x</b>, <b>--autogen-xsubs</b></b>
<p>Automatically generate XSUBs basing on function declarations in the
header file. The package <code class="inline"><span class="w">C::Scan</span></code>
should be installed. If this
option is specified, the name of the header file may look like
<code class="inline"><span class="w">NAME1</span><span class="cm">,</span><span class="w">NAME2</span></code>
. In this case NAME1 is used instead of the specified
string, but XSUBs are emitted only for the declarations included from
file NAME2.</p>
<p>Note that some types of arguments/return-values for functions may
result in XSUB-declarations/typemap-entries which need
hand-editing. Such may be objects which cannot be converted from/to a
pointer (like <code class="inline"><span class="w">long</span> <span class="w">long</span></code>
), pointers to functions, or arrays. See
also the section on <a href="http://search.cpan.org/perldoc/LIMITATIONS of <b>-x<#b%3e">"b>" in LIMITATIONS of <b>-x<</a>.</p>
</li>
</ul>
<a name="EXAMPLES"></a><h1>EXAMPLES</h1>
<pre class="verbatim"> <span class="c"># Default behavior, extension is Rusers</span>
<span class="w">h2xs</span> <span class="w">rpcsvc</span>/<span class="w">rusers</span></pre>
<pre class="verbatim"> <span class="c"># Same, but extension is RUSERS</span>
<span class="w">h2xs</span> -<span class="w">n</span> <span class="w">RUSERS</span> <span class="w">rpcsvc</span>/<span class="w">rusers</span></pre>
<pre class="verbatim"> <span class="c"># Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h></span>
<span class="w">h2xs</span> <span class="w">rpcsvc::rusers</span></pre>
<pre class="verbatim"> <span class="c"># Extension is ONC::RPC. Still finds <rpcsvc/rusers.h></span>
<span class="w">h2xs</span> -<span class="w">n</span> <span class="w">ONC::RPC</span> <span class="w">rpcsvc</span>/<span class="w">rusers</span></pre>
<pre class="verbatim"> <span class="c"># Without constant() or AUTOLOAD</span>
<span class="w">h2xs</span> -c <span class="w">rpcsvc</span>/<span class="w">rusers</span></pre>
<pre class="verbatim"> <span class="c"># Creates templates for an extension named RPC</span>
<span class="w">h2xs</span> -<span class="w">cfn</span> <span class="w">RPC</span></pre>
<pre class="verbatim"> <span class="c"># Extension is ONC::RPC.</span>
<span class="w">h2xs</span> -<span class="w">cfn</span> <span class="w">ONC::RPC</span></pre>
<pre class="verbatim"> <span class="c"># Extension is a pure Perl module with no XS code.</span>
<span class="w">h2xs</span> <a class="l_k" href="functions/-X.html">-X</a> <span class="w">My::Module</span></pre>
<pre class="verbatim"> <span class="c"># Extension is Lib::Foo which works at least with Perl5.005_03.</span>
<span class="c"># Constants are created for all #defines and enums h2xs can find</span>
<span class="c"># in foo.h.</span>
<span class="w">h2xs</span> -b <span class="v">5.5.3</span> -<span class="w">n</span> <span class="w">Lib::Foo</span> <span class="w">foo</span>.<span class="w">h</span></pre>
<pre class="verbatim"> <span class="c"># Extension is Lib::Foo which works at least with Perl5.005_03.</span>
<span class="c"># Constants are created for all #defines but only for enums</span>
<span class="c"># whose names do not start with 'bar_'.</span>
<span class="w">h2xs</span> -b <span class="v">5.5.3</span> -<span class="w">e</span> <span class="q">'^bar_'</span> -<span class="w">n</span> <span class="w">Lib::Foo</span> <span class="w">foo</span>.<span class="w">h</span></pre>
<pre class="verbatim"> <span class="c"># Makefile.PL will look for library -lrpc in</span>
<span class="c"># additional directory /opt/net/lib</span>
<span class="w">h2xs</span> <span class="w">rpcsvc</span>/<span class="w">rusers</span> -<span class="w">L</span>/<span class="w">opt</span>/<span class="w">net</span>/<span class="w">lib</span> -<span class="w">lrpc</span></pre>
<pre class="verbatim"> <span class="c"># Extension is DCE::rgynbase</span>
<span class="c"># prefix "sec_rgy_" is dropped from perl function names</span>
<span class="w">h2xs</span> -<span class="w">n</span> <span class="w">DCE::rgynbase</span> -p <span class="w">sec_rgy_</span> <span class="w">dce</span>/<span class="w">rgynbase</span></pre>
<pre class="verbatim"> <span class="c"># Extension is DCE::rgynbase</span>
<span class="c"># prefix "sec_rgy_" is dropped from perl function names</span>
<span class="c"># subroutines are created for sec_rgy_wildcard_name and</span>
<span class="c"># sec_rgy_wildcard_sid</span>
<span class="w">h2xs</span> -<span class="w">n</span> <span class="w">DCE::rgynbase</span> -p <span class="w">sec_rgy_</span> \
-s <span class="w">sec_rgy_wildcard_name</span><span class="cm">,</span><span class="w">sec_rgy_wildcard_sid</span> <span class="w">dce</span>/<span class="w">rgynbase</span></pre>
<pre class="verbatim"> # Make XS without defines in perl.h, but with function declarations
# visible from perl.h. Name of the extension is perl1.
# When scanning perl.h, define -DEXT=extern -DdEXT= -DINIT(x)=
# Extra backslashes below because the string is passed to shell.
# Note that a directory with perl header files would
# be added automatically to include path.
h2xs -xAn perl1 -F "-DEXT=extern -DdEXT= -DINIT\(x\)=" perl.h</pre><pre class="verbatim"> <span class="c"># Same with function declaration in proto.h as visible from perl.h.</span>
<span class="w">h2xs</span> -<span class="w">xAn</span> <span class="w">perl2</span> <span class="w">perl</span>.<span class="w">h</span><span class="cm">,</span><span class="w">proto</span>.<span class="w">h</span></pre>
<pre class="verbatim"> <span class="c"># Same but select only functions which match /^av_/</span>
<span class="w">h2xs</span> -M <span class="q">'^av_'</span> -<span class="w">xAn</span> <span class="w">perl2</span> <span class="w">perl</span>.<span class="w">h</span><span class="cm">,</span><span class="w">proto</span>.<span class="w">h</span></pre>
<pre class="verbatim"> <span class="c"># Same but treat SV* etc as "opaque" types</span>
<span class="w">h2xs</span> -o <span class="q">'^[S]V \*$'</span> -<span class="w">M</span> <span class="q">'^av_'</span> -<span class="w">xAn</span> <span class="w">perl2</span> <span class="w">perl</span>.<span class="w">h</span><span class="cm">,</span><span class="w">proto</span>.<span class="w">h</span></pre>
<a name="Extension-based-on-_.h_-and-_.c_-files"></a><h2>Extension based on <i>.h</i> and <i>.c</i> files</h2>
<p>Suppose that you have some C files implementing some functionality,
and the corresponding header files. How to create an extension which
makes this functionality accessible in Perl? The example below
assumes that the header files are <i>interface_simple.h</i> and
<i>interface_hairy.h</i>, and you want the perl module be named as
<code class="inline"><span class="w">Ext::Ension</span></code>
. If you need some preprocessor directives and/or
linking with external libraries, see the flags <code class="inline">-<span class="w">F</span></code>
, <code class="inline">-<span class="w">L</span></code>
and <code class="inline">-l</code>
in <a href="#OPTIONS">"OPTIONS"</a>.</p>
<ul>
<li><a name="Find-the-directory-name"></a><b>Find the directory name</b>
<p>Start with a dummy run of h2xs:</p>
<pre class="verbatim"> <span class="w">h2xs</span> -<span class="w">Afn</span> <span class="w">Ext::Ension</span></pre>
<p>The only purpose of this step is to create the needed directories, and
let you know the names of these directories. From the output you can
see that the directory for the extension is <i>Ext/Ension</i>.</p>
</li>
<li><a name="Copy-C-files"></a><b>Copy C files</b>
<p>Copy your header files and C files to this directory <i>Ext/Ension</i>.</p>
</li>
<li><a name="Create-the-extension"></a><b>Create the extension</b>
<p>Run h2xs, overwriting older autogenerated files:</p>
<pre class="verbatim"> <span class="w">h2xs</span> -<span class="w">Oxan</span> <span class="w">Ext::Ension</span> <span class="w">interface_simple</span>.<span class="w">h</span> <span class="w">interface_hairy</span>.<span class="w">h</span></pre>
<p>h2xs looks for header files <i>after</i> changing to the extension
directory, so it will find your header files OK.</p>
</li>
<li><a name="Archive-and-test"></a><b>Archive and test</b>
<p>As usual, run</p>
<pre class="verbatim"> <span class="w">cd</span> <span class="w">Ext</span>/<span class="w">Ension</span>
<span class="w">perl</span> <span class="w">Makefile</span>.<span class="w">PL</span>
<span class="w">make</span> <span class="w">dist</span>
<span class="w">make</span>
<span class="w">make</span> <span class="w">test</span></pre>
</li>
<li><a name="Hints"></a><b>Hints</b>
<p>It is important to do <code class="inline"><span class="w">make</span> <span class="w">dist</span></code>
as early as possible. This way you
can easily merge(1) your changes to autogenerated files if you decide
to edit your <code class="inline">.h</code> files and rerun h2xs.</p>
<p>Do not forget to edit the documentation in the generated <i>.pm</i> file.</p>
<p>Consider the autogenerated files as skeletons only, you may invent
better interfaces than what h2xs could guess.</p>
<p>Consider this section as a guideline only, some other options of h2xs
may better suit your needs.</p>
</li>
</ul>
<a name="ENVIRONMENT"></a><h1>ENVIRONMENT</h1>
<p>No environment variables are used.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Larry Wall and others</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="perl.html">perl</a>, <a href="perlxstut.html">perlxstut</a>, <a href="ExtUtils/MakeMaker.html">ExtUtils::MakeMaker</a>, and <a href="AutoLoader.html">AutoLoader</a>.</p>
<a name="DIAGNOSTICS"></a><h1>DIAGNOSTICS</h1>
<p>The usual warnings if it cannot read or write the files involved.</p>
<a name="LIMITATIONS-of-*-x*"></a><h1>LIMITATIONS of <b>-x</b></h1>
<p><i>h2xs</i> would not distinguish whether an argument to a C function
which is of the form, say, <code class="inline"><a class="l_k" href="functions/int.html">int</a> <span class="i">*</span></code>
, is an input, output, or
input/output parameter. In particular, argument declarations of the
form</p>
<pre class="verbatim"> <a class="l_k" href="functions/int.html">int</a>
<span class="i">foo</span><span class="s">(</span><span class="w">n</span><span class="s">)</span>
<a class="l_k" href="functions/int.html">int</a> <span class="i">*n</span></pre>
<p>should be better rewritten as</p>
<pre class="verbatim"> <a class="l_k" href="functions/int.html">int</a>
<span class="i">foo</span><span class="s">(</span><span class="w">n</span><span class="s">)</span>
<a class="l_k" href="functions/int.html">int</a> <span class="i">&n</span></pre>
<p>if <code class="inline"><span class="w">n</span></code>
is an input parameter.</p>
<p>Additionally, <i>h2xs</i> has no facilities to intuit that a function</p>
<pre class="verbatim"> int
foo(addr,l)
char *addr
int l</pre><p>takes a pair of address and length of data at this address, so it is better
to rewrite this function as</p>
<pre class="verbatim"> int
foo(sv)
SV *addr
PREINIT:
STRLEN len;
char *s;
CODE:
s = SvPV(sv,len);
RETVAL = foo(s, len);
OUTPUT:
RETVAL</pre><p>or alternately</p>
<pre class="verbatim"> static int
my_foo(SV *sv)
{
STRLEN len;
char *s = SvPV(sv,len);</pre><pre class="verbatim"> return foo(s, len);
}</pre><pre class="verbatim"> <span class="w">MODULE</span> = <span class="w">foo</span> <span class="w">PACKAGE</span> = <span class="w">foo</span> <span class="w">PREFIX</span> = <span class="w">my_</span></pre>
<pre class="verbatim"> int
foo(sv)
SV *sv</pre><p>See <a href="perlxs.html">perlxs</a> and <a href="perlxstut.html">perlxstut</a> for additional details.</p>
</div>
<div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
</div>
</div>
<div id="right">
<div id="rightContent">
<div id="leftClose">
<a href="#" onClick="closeRight()" title="Hide toolbar" onmouseover="rightCloseIcon.src='close_purple.gif';" onmouseout="rightCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="rightCloseIcon" id="rightCloseIcon" border=0></a>
</div>
<h1>Search:</h1>
<p>
<form action="search.html" name="perldoc_search">
<input type="text" name="q" size="10" class="grey"><br>
<!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
</form>
</p>
<script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
<h2>Labels:</h2>
<p>
<a href="#" onClick="addLabel('h2xs','h2xs.html')">Add this page</a>
</p>
<div class="labels" id="labels">
</div>
</div>
</div>
</div>
</body>
</html>
|