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