1H2XS(1) Perl Programmers Reference Guide H2XS(1)
2
3
4
6 h2xs - convert .h C header files to Perl extensions
7
9 h2xs [OPTIONS ...] [headerfile ... [extra_libraries]]
10
11 h2xs -h|-?|--help
12
14 h2xs builds a Perl extension from C header files. The extension will
15 include functions which can be used to retrieve the value of any
16 #define statement which was in the C header files.
17
18 The module_name will be used for the name of the extension. If
19 module_name is not supplied then the name of the first header file will
20 be used, with the first character capitalized.
21
22 If the extension might need extra libraries, they should be included
23 here. The extension Makefile.PL will take care of checking whether the
24 libraries actually exist and how they should be loaded. The extra
25 libraries should be specified in the form -lm -lposix, etc, just as on
26 the cc command line. By default, the Makefile.PL will search through
27 the library path determined by Configure. That path can be augmented
28 by including arguments of the form -L/another/library/path in the
29 extra-libraries argument.
30
31 In spite of its name, h2xs may also be used to create a skeleton pure
32 Perl module. See the -X option.
33
35 -A, --omit-autoload
36 Omit all autoload facilities. This is the same as -c but also
37 removes the "use AutoLoader" statement from the .pm file.
38
39 -B, --beta-version
40 Use an alpha/beta style version number. Causes version number to
41 be "0.00_01" unless -v is specified.
42
43 -C, --omit-changes
44 Omits creation of the Changes file, and adds a HISTORY section to
45 the POD template.
46
47 -F, --cpp-flags=addflags
48 Additional flags to specify to C preprocessor when scanning header
49 for function declarations. Writes these options in the generated
50 Makefile.PL too.
51
52 -M, --func-mask=regular expression
53 selects functions/macros to process.
54
55 -O, --overwrite-ok
56 Allows a pre-existing extension directory to be overwritten.
57
58 -P, --omit-pod
59 Omit the autogenerated stub POD section.
60
61 -X, --omit-XS
62 Omit the XS portion. Used to generate a skeleton pure Perl module.
63 "-c" and "-f" are implicitly enabled.
64
65 -a, --gen-accessors
66 Generate an accessor method for each element of structs and
67 unions. The generated methods are named after the element name;
68 will return the current value of the element if called without
69 additional arguments; and will set the element to the supplied
70 value (and return the new value) if called with an additional
71 argument. Embedded structures and unions are returned as a pointer
72 rather than the complete structure, to facilitate chained calls.
73
74 These methods all apply to the Ptr type for the structure;
75 additionally two methods are constructed for the structure type
76 itself, "_to_ptr" which returns a Ptr type pointing to the same
77 structure, and a "new" method to construct and return a new
78 structure, initialised to zeroes.
79
80 -b, --compat-version=version
81 Generates a .pm file which is backwards compatible with the
82 specified perl version.
83
84 For versions < 5.6.0, the changes are.
85 - no use of 'our' (uses 'use vars' instead)
86 - no 'use warnings'
87
88 Specifying a compatibility version higher than the version of perl
89 you are using to run h2xs will have no effect. If unspecified
90 h2xs will default to compatibility with the version of perl you
91 are using to run h2xs.
92
93 -c, --omit-constant
94 Omit "constant()" from the .xs file and corresponding specialised
95 "AUTOLOAD" from the .pm file.
96
97 -d, --debugging
98 Turn on debugging messages.
99
100 -e, --omit-enums=[regular expression]
101 If regular expression is not given, skip all constants that are
102 defined in a C enumeration. Otherwise skip only those constants
103 that are defined in an enum whose name matches regular expression.
104
105 Since regular expression is optional, make sure that this switch
106 is followed by at least one other switch if you omit regular
107 expression and have some pending arguments such as header-file
108 names. This is ok:
109
110 h2xs -e -n Module::Foo foo.h
111
112 This is not ok:
113
114 h2xs -n Module::Foo -e foo.h
115
116 In the latter, foo.h is taken as regular expression.
117
118 -f, --force
119 Allows an extension to be created for a header even if that header
120 is not found in standard include directories.
121
122 -g, --global
123 Include code for safely storing static data in the .xs file.
124 Extensions that do no make use of static data can ignore this
125 option.
126
127 -h, -?, --help
128 Print the usage, help and version for this h2xs and exit.
129
130 -k, --omit-const-func
131 For function arguments declared as "const", omit the const
132 attribute in the generated XS code.
133
134 -m, --gen-tied-var
135 Experimental: for each variable declared in the header file(s),
136 declare a perl variable of the same name magically tied to the C
137 variable.
138
139 -n, --name=module_name
140 Specifies a name to be used for the extension, e.g., -n RPC::DCE
141
142 -o, --opaque-re=regular expression
143 Use "opaque" data type for the C types matched by the regular
144 expression, even if these types are "typedef"-equivalent to types
145 from typemaps. Should not be used without -x.
146
147 This may be useful since, say, types which are
148 "typedef"-equivalent to integers may represent OS-related handles,
149 and one may want to work with these handles in OO-way, as in
150 "$handle->do_something()". Use "-o ." if you want to handle all
151 the "typedef"ed types as opaque types.
152
153 The type-to-match is whitewashed (except for commas, which have no
154 whitespace before them, and multiple "*" which have no whitespace
155 between them).
156
157 -p, --remove-prefix=prefix
158 Specify a prefix which should be removed from the Perl function
159 names, e.g., -p sec_rgy_ This sets up the XS PREFIX keyword and
160 removes the prefix from functions that are autoloaded via the
161 "constant()" mechanism.
162
163 -s, --const-subs=sub1,sub2
164 Create a perl subroutine for the specified macros rather than
165 autoload with the constant() subroutine. These macros are assumed
166 to have a return type of char *, e.g.,
167 -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid.
168
169 -t, --default-type=type
170 Specify the internal type that the constant() mechanism uses for
171 macros. The default is IV (signed integer). Currently all macros
172 found during the header scanning process will be assumed to have
173 this type. Future versions of "h2xs" may gain the ability to make
174 educated guesses.
175
176 --use-new-tests
177 When --compat-version (-b) is present the generated tests will use
178 "Test::More" rather than "Test" which is the default for versions
179 before 5.6.2. "Test::More" will be added to PREREQ_PM in the
180 generated "Makefile.PL".
181
182 --use-old-tests
183 Will force the generation of test code that uses the older "Test"
184 module.
185
186 --skip-exporter
187 Do not use "Exporter" and/or export any symbol.
188
189 --skip-ppport
190 Do not use "Devel::PPPort": no portability to older version.
191
192 --skip-autoloader
193 Do not use the module "AutoLoader"; but keep the constant()
194 function and "sub AUTOLOAD" for constants.
195
196 --skip-strict
197 Do not use the pragma "strict".
198
199 --skip-warnings
200 Do not use the pragma "warnings".
201
202 -v, --version=version
203 Specify a version number for this extension. This version number
204 is added to the templates. The default is 0.01, or 0.00_01 if
205 "-B" is specified. The version specified should be numeric.
206
207 -x, --autogen-xsubs
208 Automatically generate XSUBs basing on function declarations in
209 the header file. The package "C::Scan" should be installed. If
210 this option is specified, the name of the header file may look
211 like "NAME1,NAME2". In this case NAME1 is used instead of the
212 specified string, but XSUBs are emitted only for the declarations
213 included from file NAME2.
214
215 Note that some types of arguments/return-values for functions may
216 result in XSUB-declarations/typemap-entries which need hand-
217 editing. Such may be objects which cannot be converted from/to a
218 pointer (like "long long"), pointers to functions, or arrays. See
219 also the section on "LIMITATIONS of -x".
220
222 # Default behavior, extension is Rusers
223 h2xs rpcsvc/rusers
224
225 # Same, but extension is RUSERS
226 h2xs -n RUSERS rpcsvc/rusers
227
228 # Extension is rpcsvc::rusers. Still finds <rpcsvc/rusers.h>
229 h2xs rpcsvc::rusers
230
231 # Extension is ONC::RPC. Still finds <rpcsvc/rusers.h>
232 h2xs -n ONC::RPC rpcsvc/rusers
233
234 # Without constant() or AUTOLOAD
235 h2xs -c rpcsvc/rusers
236
237 # Creates templates for an extension named RPC
238 h2xs -cfn RPC
239
240 # Extension is ONC::RPC.
241 h2xs -cfn ONC::RPC
242
243 # Extension is a pure Perl module with no XS code.
244 h2xs -X My::Module
245
246 # Extension is Lib::Foo which works at least with Perl5.005_03.
247 # Constants are created for all #defines and enums h2xs can find
248 # in foo.h.
249 h2xs -b 5.5.3 -n Lib::Foo foo.h
250
251 # Extension is Lib::Foo which works at least with Perl5.005_03.
252 # Constants are created for all #defines but only for enums
253 # whose names do not start with 'bar_'.
254 h2xs -b 5.5.3 -e '^bar_' -n Lib::Foo foo.h
255
256 # Makefile.PL will look for library -lrpc in
257 # additional directory /opt/net/lib
258 h2xs rpcsvc/rusers -L/opt/net/lib -lrpc
259
260 # Extension is DCE::rgynbase
261 # prefix "sec_rgy_" is dropped from perl function names
262 h2xs -n DCE::rgynbase -p sec_rgy_ dce/rgynbase
263
264 # Extension is DCE::rgynbase
265 # prefix "sec_rgy_" is dropped from perl function names
266 # subroutines are created for sec_rgy_wildcard_name and
267 # sec_rgy_wildcard_sid
268 h2xs -n DCE::rgynbase -p sec_rgy_ \
269 -s sec_rgy_wildcard_name,sec_rgy_wildcard_sid dce/rgynbase
270
271 # Make XS without defines in perl.h, but with function declarations
272 # visible from perl.h. Name of the extension is perl1.
273 # When scanning perl.h, define -DEXT=extern -DdEXT= -DINIT(x)=
274 # Extra backslashes below because the string is passed to shell.
275 # Note that a directory with perl header files would
276 # be added automatically to include path.
277 h2xs -xAn perl1 -F "-DEXT=extern -DdEXT= -DINIT\(x\)=" perl.h
278
279 # Same with function declaration in proto.h as visible from perl.h.
280 h2xs -xAn perl2 perl.h,proto.h
281
282 # Same but select only functions which match /^av_/
283 h2xs -M '^av_' -xAn perl2 perl.h,proto.h
284
285 # Same but treat SV* etc as "opaque" types
286 h2xs -o '^[S]V \*$' -M '^av_' -xAn perl2 perl.h,proto.h
287
288 Extension based on .h and .c files
289 Suppose that you have some C files implementing some functionality, and
290 the corresponding header files. How to create an extension which makes
291 this functionality accessible in Perl? The example below assumes that
292 the header files are interface_simple.h and interface_hairy.h, and you
293 want the perl module be named as "Ext::Ension". If you need some
294 preprocessor directives and/or linking with external libraries, see the
295 flags "-F", "-L" and "-l" in "OPTIONS".
296
297 Find the directory name
298 Start with a dummy run of h2xs:
299
300 h2xs -Afn Ext::Ension
301
302 The only purpose of this step is to create the needed directories,
303 and let you know the names of these directories. From the output
304 you can see that the directory for the extension is Ext/Ension.
305
306 Copy C files
307 Copy your header files and C files to this directory Ext/Ension.
308
309 Create the extension
310 Run h2xs, overwriting older autogenerated files:
311
312 h2xs -Oxan Ext::Ension interface_simple.h interface_hairy.h
313
314 h2xs looks for header files after changing to the extension
315 directory, so it will find your header files OK.
316
317 Archive and test
318 As usual, run
319
320 cd Ext/Ension
321 perl Makefile.PL
322 make dist
323 make
324 make test
325
326 Hints
327 It is important to do "make dist" as early as possible. This way
328 you can easily merge(1) your changes to autogenerated files if you
329 decide to edit your ".h" files and rerun h2xs.
330
331 Do not forget to edit the documentation in the generated .pm file.
332
333 Consider the autogenerated files as skeletons only, you may invent
334 better interfaces than what h2xs could guess.
335
336 Consider this section as a guideline only, some other options of
337 h2xs may better suit your needs.
338
340 No environment variables are used.
341
343 Larry Wall and others
344
346 perl, perlxstut, ExtUtils::MakeMaker, and AutoLoader.
347
349 The usual warnings if it cannot read or write the files involved.
350
352 h2xs would not distinguish whether an argument to a C function which is
353 of the form, say, "int *", is an input, output, or input/output
354 parameter. In particular, argument declarations of the form
355
356 int
357 foo(n)
358 int *n
359
360 should be better rewritten as
361
362 int
363 foo(n)
364 int &n
365
366 if "n" is an input parameter.
367
368 Additionally, h2xs has no facilities to intuit that a function
369
370 int
371 foo(addr,l)
372 char *addr
373 int l
374
375 takes a pair of address and length of data at this address, so it is
376 better to rewrite this function as
377
378 int
379 foo(sv)
380 SV *addr
381 PREINIT:
382 STRLEN len;
383 char *s;
384 CODE:
385 s = SvPV(sv,len);
386 RETVAL = foo(s, len);
387 OUTPUT:
388 RETVAL
389
390 or alternately
391
392 static int
393 my_foo(SV *sv)
394 {
395 STRLEN len;
396 char *s = SvPV(sv,len);
397
398 return foo(s, len);
399 }
400
401 MODULE = foo PACKAGE = foo PREFIX = my_
402
403 int
404 foo(sv)
405 SV *sv
406
407 See perlxs and perlxstut for additional details.
408
409
410
411perl v5.32.1 2021-05-31 H2XS(1)