man-pages/man1/h2xs.1.html
2021-03-31 01:06:50 +01:00

679 lines
17 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of H2XS</TITLE>
</HEAD><BODY>
<H1>H2XS</H1>
Section: Perl Programmers Reference Guide (1)<BR>Updated: 2020-10-19<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
h2xs - convert .h C header files to Perl extensions
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>h2xs</B> [<B></B><FONT SIZE="-1"><B>OPTIONS</B></FONT><B></B> ...] [headerfile ... [extra_libraries]]
<P>
<B>h2xs</B> <B>-h</B>|<B>-?</B>|<B>--help</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<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>
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>
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>
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.
<A NAME="lbAE">&nbsp;</A>
<H2>OPTIONS</H2>
<DL COMPACT>
<DT id="1"><B>-A</B>, <B>--omit-autoload</B><DD>
Omit all autoload facilities. This is the same as <B>-c</B> but also
removes the <TT>&quot;use AutoLoader&quot;</TT> statement from the .pm file.
<DT id="2"><B>-B</B>, <B>--beta-version</B><DD>
Use an alpha/beta style version number. Causes version number to
be ``0.00_01'' unless <B>-v</B> is specified.
<DT id="3"><B>-C</B>, <B>--omit-changes</B><DD>
Omits creation of the <I>Changes</I> file, and adds a <FONT SIZE="-1">HISTORY</FONT> section to
the <FONT SIZE="-1">POD</FONT> template.
<DT id="4"><B>-F</B>, <B>--cpp-flags</B>=<I>addflags</I><DD>
Additional flags to specify to C preprocessor when scanning header for
function declarations. Writes these options in the generated <I>Makefile.PL</I>
too.
<DT id="5"><B>-M</B>, <B>--func-mask</B>=<I>regular expression</I><DD>
selects functions/macros to process.
<DT id="6"><B>-O</B>, <B>--overwrite-ok</B><DD>
Allows a pre-existing extension directory to be overwritten.
<DT id="7"><B>-P</B>, <B>--omit-pod</B><DD>
Omit the autogenerated stub <FONT SIZE="-1">POD</FONT> section.
<DT id="8"><B>-X</B>, <B>--omit-XS</B><DD>
Omit the <FONT SIZE="-1">XS</FONT> portion. Used to generate a skeleton pure Perl module.
<TT>&quot;-c&quot;</TT> and <TT>&quot;-f&quot;</TT> are implicitly enabled.
<DT id="9"><B>-a</B>, <B>--gen-accessors</B><DD>
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>
These methods all apply to the Ptr type for the structure; additionally
two methods are constructed for the structure type itself, <TT>&quot;_to_ptr&quot;</TT>
which returns a Ptr type pointing to the same structure, and a <TT>&quot;new&quot;</TT>
method to construct and return a new structure, initialised to zeroes.
<DT id="10"><B>-b</B>, <B>--compat-version</B>=<I>version</I><DD>
Generates a .pm file which is backwards compatible with the specified
perl version.
<P>
For versions &lt; 5.6.0, the changes are.
<BR>&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;no&nbsp;use&nbsp;of&nbsp;'our'&nbsp;(uses&nbsp;'use&nbsp;vars'&nbsp;instead)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;no&nbsp;'use&nbsp;warnings'
<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.
<DT id="11"><B>-c</B>, <B>--omit-constant</B><DD>
Omit <TT>&quot;constant()&quot;</TT> from the .xs file and corresponding specialised
<TT>&quot;AUTOLOAD&quot;</TT> from the .pm file.
<DT id="12"><B>-d</B>, <B>--debugging</B><DD>
Turn on debugging messages.
<DT id="13"><B>-e</B>, <B>--omit-enums</B>=[<I>regular expression</I>]<DD>
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>
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>
h2xs -e -n Module::Foo foo.h
</PRE>
<P>
This is not ok:
<P>
<PRE>
h2xs -n Module::Foo -e foo.h
</PRE>
<P>
In the latter, foo.h is taken as <I>regular expression</I>.
<DT id="14"><B>-f</B>, <B>--force</B><DD>
Allows an extension to be created for a header even if that header is
not found in standard include directories.
<DT id="15"><B>-g</B>, <B>--global</B><DD>
Include code for safely storing static data in the .xs file.
Extensions that do no make use of static data can ignore this option.
<DT id="16"><B>-h</B>, <B>-?</B>, <B>--help</B><DD>
Print the usage, help and version for this h2xs and exit.
<DT id="17"><B>-k</B>, <B>--omit-const-func</B><DD>
For function arguments declared as <TT>&quot;const&quot;</TT>, omit the const attribute in the
generated <FONT SIZE="-1">XS</FONT> code.
<DT id="18"><B>-m</B>, <B>--gen-tied-var</B><DD>
<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.
<DT id="19"><B>-n</B>, <B>--name</B>=<I>module_name</I><DD>
Specifies a name to be used for the extension, e.g., -n RPC::DCE
<DT id="20"><B>-o</B>, <B>--opaque-re</B>=<I>regular expression</I><DD>
Use ``opaque'' data type for the C types matched by the regular
expression, even if these types are <TT>&quot;typedef&quot;</TT>-equivalent to types
from typemaps. Should not be used without <B>-x</B>.
<P>
This may be useful since, say, types which are <TT>&quot;typedef&quot;</TT>-equivalent
to integers may represent OS-related handles, and one may want to work
with these handles in OO-way, as in <TT>&quot;$handle-&gt;do_something()&quot;</TT>.
Use <TT>&quot;-o .&quot;</TT> if you want to handle all the <TT>&quot;typedef&quot;</TT>ed types as opaque
types.
<P>
The type-to-match is whitewashed (except for commas, which have no
whitespace before them, and multiple <TT>&quot;*&quot;</TT> which have no whitespace
between them).
<DT id="21"><B>-p</B>, <B>--remove-prefix</B>=<I>prefix</I><DD>
Specify a prefix which should be removed from the Perl function names,
e.g., -p sec_rgy_ This sets up the <FONT SIZE="-1">XS</FONT> <B></B><FONT SIZE="-1"><B>PREFIX</B></FONT><B></B> keyword and removes
the prefix from functions that are autoloaded via the <TT>&quot;constant()&quot;</TT>
mechanism.
<DT id="22"><B>-s</B>, <B>--const-subs</B>=<I>sub1,sub2</I><DD>
Create a perl subroutine for the specified macros rather than autoload
with the <B>constant()</B> 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.
<DT id="23"><B>-t</B>, <B>--default-type</B>=<I>type</I><DD>
Specify the internal type that the <B>constant()</B> mechanism uses for macros.
The default is <FONT SIZE="-1">IV</FONT> (signed integer). Currently all macros found during the
header scanning process will be assumed to have this type. Future versions
of <TT>&quot;h2xs&quot;</TT> may gain the ability to make educated guesses.
<DT id="24"><B>--use-new-tests</B><DD>
When <B>--compat-version</B> (<B>-b</B>) is present the generated tests will use
<TT>&quot;Test::More&quot;</TT> rather than <TT>&quot;Test&quot;</TT> which is the default for versions before
5.6.2. <TT>&quot;Test::More&quot;</TT> will be added to <FONT SIZE="-1">PREREQ_PM</FONT> in the generated
<TT>&quot;Makefile.PL&quot;</TT>.
<DT id="25"><B>--use-old-tests</B><DD>
Will force the generation of test code that uses the older <TT>&quot;Test&quot;</TT> module.
<DT id="26"><B>--skip-exporter</B><DD>
Do not use <TT>&quot;Exporter&quot;</TT> and/or export any symbol.
<DT id="27"><B>--skip-ppport</B><DD>
Do not use <TT>&quot;Devel::PPPort&quot;</TT>: no portability to older version.
<DT id="28"><B>--skip-autoloader</B><DD>
Do not use the module <TT>&quot;AutoLoader&quot;</TT>; but keep the <B>constant()</B> function
and <TT>&quot;sub AUTOLOAD&quot;</TT> for constants.
<DT id="29"><B>--skip-strict</B><DD>
Do not use the pragma <TT>&quot;strict&quot;</TT>.
<DT id="30"><B>--skip-warnings</B><DD>
Do not use the pragma <TT>&quot;warnings&quot;</TT>.
<DT id="31"><B>-v</B>, <B>--version</B>=<I>version</I><DD>
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 <TT>&quot;-B&quot;</TT> is specified.
The version specified should be numeric.
<DT id="32"><B>-x</B>, <B>--autogen-xsubs</B><DD>
Automatically generate XSUBs basing on function declarations in the
header file. The package <TT>&quot;C::Scan&quot;</TT> should be installed. If this
option is specified, the name of the header file may look like
<TT>&quot;NAME1,NAME2&quot;</TT>. In this case <FONT SIZE="-1">NAME1</FONT> is used instead of the specified
string, but XSUBs are emitted only for the declarations included from
file <FONT SIZE="-1">NAME2.</FONT>
<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 <TT>&quot;long long&quot;</TT>), pointers to functions, or arrays. See
also the section on &quot;<FONT SIZE="-1">LIMITATIONS</FONT> of <B>-x</B>&quot;.
</DL>
<A NAME="lbAF">&nbsp;</A>
<H2>EXAMPLES</H2>
<PRE>
# Default behavior, extension is Rusers
h2xs rpcsvc/rusers
# Same, but extension is RUSERS
h2xs -n RUSERS rpcsvc/rusers
# Extension is rpcsvc::rusers. Still finds &lt;<A HREF="file:///usr/include/rpcsvc/rusers.h">rpcsvc/rusers.h</A>&gt;
h2xs rpcsvc::rusers
# Extension is ONC::RPC. Still finds &lt;<A HREF="file:///usr/include/rpcsvc/rusers.h">rpcsvc/rusers.h</A>&gt;
h2xs -n ONC::RPC rpcsvc/rusers
# Without constant() or AUTOLOAD
h2xs -c rpcsvc/rusers
# Creates templates for an extension named RPC
h2xs -cfn RPC
# Extension is ONC::RPC.
h2xs -cfn ONC::RPC
# Extension is a pure Perl module with no XS code.
h2xs -X My::Module
# Extension is Lib::Foo which works at least with Perl5.005_03.
# Constants are created for all #defines and enums h2xs can find
# in foo.h.
h2xs -b 5.5.3 -n Lib::Foo foo.h
# Extension is Lib::Foo which works at least with Perl5.005_03.
# Constants are created for all #defines but only for enums
# whose names do not start with 'bar_'.
h2xs -b 5.5.3 -e '^bar_' -n Lib::Foo foo.h
# Makefile.PL will look for library -lrpc in
# additional directory /opt/net/lib
h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
# Extension is DCE::rgynbase
# prefix &quot;sec_rgy_&quot; is dropped from perl function names
h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
# Extension is DCE::rgynbase
# prefix &quot;sec_rgy_&quot; is dropped from perl function names
# subroutines are created for sec_rgy_wildcard_name and
# sec_rgy_wildcard_sid
h2xs -n DCE::rgynbase -p sec_rgy_ \
-s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
# 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 &quot;-DEXT=extern -DdEXT= -DINIT\(x\)=&quot; perl.h
# Same with function declaration in proto.h as visible from perl.h.
h2xs -xAn perl2 perl.h,proto.h
# Same but select only functions which match /^av_/
h2xs -M '^av_' -xAn perl2 perl.h,proto.h
# Same but treat SV* etc as &quot;opaque&quot; types
h2xs -o '^[S]V \*$' -M '^av_' -xAn perl2 perl.h,proto.h
</PRE>
<A NAME="lbAG">&nbsp;</A>
<H3>Extension based on <I>.h</I> and <I>.c</I> files</H3>
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
<TT>&quot;Ext::Ension&quot;</TT>. If you need some preprocessor directives and/or
linking with external libraries, see the flags <TT>&quot;-F&quot;</TT>, <TT>&quot;-L&quot;</TT> and <TT>&quot;-l&quot;</TT>
in ``<FONT SIZE="-1">OPTIONS''</FONT>.
<DL COMPACT>
<DT id="33">Find the directory name<DD>
Start with a dummy run of h2xs:
<P>
<PRE>
h2xs -Afn Ext::Ension
</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>.
<DT id="34">Copy C files<DD>
Copy your header files and C files to this directory <I>Ext/Ension</I>.
<DT id="35">Create the extension<DD>
Run h2xs, overwriting older autogenerated files:
<P>
<PRE>
h2xs -Oxan Ext::Ension interface_simple.h interface_hairy.h
</PRE>
<P>
h2xs looks for header files <I>after</I> changing to the extension
directory, so it will find your header files <FONT SIZE="-1">OK.</FONT>
<DT id="36">Archive and test<DD>
As usual, run
<P>
<PRE>
cd Ext/Ension
perl Makefile.PL
make dist
make
make test
</PRE>
<DT id="37">Hints<DD>
It is important to do <TT>&quot;make dist&quot;</TT> as early as possible. This way you
can easily <B><A HREF="/cgi-bin/man/man2html?1+merge">merge</A></B>(1) your changes to autogenerated files if you decide
to edit your <TT>&quot;.h&quot;</TT> files and rerun h2xs.
<P>
Do not forget to edit the documentation in the generated <I>.pm</I> file.
<P>
Consider the autogenerated files as skeletons only, you may invent
better interfaces than what h2xs could guess.
<P>
Consider this section as a guideline only, some other options of h2xs
may better suit your needs.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>ENVIRONMENT</H2>
No environment variables are used.
<A NAME="lbAI">&nbsp;</A>
<H2>AUTHOR</H2>
Larry Wall and others
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
perl, perlxstut, ExtUtils::MakeMaker, and AutoLoader.
<A NAME="lbAK">&nbsp;</A>
<H2>DIAGNOSTICS</H2>
The usual warnings if it cannot read or write the files involved.
<A NAME="lbAL">&nbsp;</A>
<H2>LIMITATIONS of <B>-x</B></H2>
<I>h2xs</I> would not distinguish whether an argument to a C function
which is of the form, say, <TT>&quot;int *&quot;</TT>, is an input, output, or
input/output parameter. In particular, argument declarations of the
form
<P>
<PRE>
int
<A HREF="/cgi-bin/man/man2html?n+foo">foo</A>(n)
int *n
</PRE>
<P>
should be better rewritten as
<P>
<PRE>
int
<A HREF="/cgi-bin/man/man2html?n+foo">foo</A>(n)
int &amp;n
</PRE>
<P>
if <TT>&quot;n&quot;</TT> is an input parameter.
<P>
Additionally, <I>h2xs</I> has no facilities to intuit that a function
<P>
<PRE>
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>
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>
static int
my_foo(SV *sv)
{
STRLEN len;
char *s = SvPV(sv,len);
return foo(s, len);
}
MODULE = foo PACKAGE = foo PREFIX = my_
int
foo(sv)
SV *sv
</PRE>
<P>
See perlxs and perlxstut for additional details.
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="38"><A HREF="#lbAB">NAME</A><DD>
<DT id="39"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="40"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="41"><A HREF="#lbAE">OPTIONS</A><DD>
<DT id="42"><A HREF="#lbAF">EXAMPLES</A><DD>
<DL>
<DT id="43"><A HREF="#lbAG">Extension based on <I>.h</I> and <I>.c</I> files</A><DD>
</DL>
<DT id="44"><A HREF="#lbAH">ENVIRONMENT</A><DD>
<DT id="45"><A HREF="#lbAI">AUTHOR</A><DD>
<DT id="46"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="47"><A HREF="#lbAK">DIAGNOSTICS</A><DD>
<DT id="48"><A HREF="#lbAL">LIMITATIONS of <B>-x</B></A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:16 GMT, March 31, 2021
</BODY>
</HTML>