1NAME
2
3 Inline::C - C Language Support for Inline
4
5VERSION
6
7 This document describes Inline::C version 0.81.
8
9DESCRIPTION
10
11 Inline::C is a module that allows you to write Perl subroutines in C.
12 Since version 0.30 the Inline module supports multiple programming
13 languages and each language has its own support module. This document
14 describes how to use Inline with the C programming language. It also
15 goes a bit into Perl C internals.
16
17 If you want to start working with programming examples right away,
18 check out Inline::C::Cookbook. For more information on Inline in
19 general, see Inline.
20
21USAGE
22
23 You never actually use Inline::C directly. It is just a support module
24 for using Inline.pm with C. So the usage is always:
25
26 use Inline C => ...;
27
28 or
29
30 bind Inline C => ...;
31
32FUNCTION DEFINITIONS
33
34 The Inline grammar for C recognizes certain function definitions (or
35 signatures) in your C code. If a signature is recognized by Inline,
36 then it will be available in Perl-space. That is, Inline will generate
37 the "glue" necessary to call that function as if it were a Perl
38 subroutine. If the signature is not recognized, Inline will simply
39 ignore it, with no complaints. It will not be available from
40 Perl-space, although it will be available from C-space.
41
42 Inline looks for ANSI/prototype style function definitions. They must
43 be of the form:
44
45 return-type function-name ( type-name-pairs ) { ... }
46
47 The most common types are: int, long, double, char*, and SV*. But you
48 can use any type for which Inline can find a typemap. Inline uses the
49 typemap file distributed with Perl as the default. You can specify more
50 typemaps with the typemaps configuration option.
51
52 A return type of void may also be used. The following are examples of
53 valid function definitions.
54
55 int Foo(double num, char* str) {
56 void Foo(double num, char* str) {
57 void Foo(SV*, ...) {
58 long Foo(int i, int j, ...) {
59 SV* Foo(void) { # 'void' arg invalid with the ParseRecDescent parser.
60 # Works only with the ParseRegExp parser.
61 # See the section on `using` (below).
62 SV* Foo() { # Alternative to specifying 'void' arg. Is valid with
63 # both the ParseRecDescent and ParseRegExp parsers.
64
65 The following definitions would not be recognized:
66
67 Foo(int i) { # no return type
68 int Foo(float f) { # no (default) typemap for float
69 int Foo(num, str) double num; char* str; {
70
71 Notice that Inline only looks for function definitions, not function
72 prototypes. Definitions are the syntax directly preceding a function
73 body. Also Inline does not scan external files, like headers. Only the
74 code passed to Inline is used to create bindings; although other
75 libraries can linked in, and called from C-space.
76
77C CONFIGURATION OPTIONS
78
79 For information on how to specify Inline configuration options, see
80 Inline. This section describes each of the configuration options
81 available for C. Most of the options correspond either to MakeMaker or
82 XS options of the same name. See ExtUtils::MakeMaker and perlxs.
83
84 auto_include
85
86 Specifies extra statements to automatically included. They will be
87 added onto the defaults. A newline char will be automatically added.
88
89 use Inline C => config => auto_include => '#include "yourheader.h"';
90
91 autowrap
92
93 If you enable => autowrap, Inline::C will parse function declarations
94 (prototype statements) in your C code. For each declaration it can
95 bind to, it will create a dummy wrapper that will call the real
96 function which may be in an external library. This is a nice
97 convenience for functions that would otherwise just require an empty
98 wrapper function.
99
100 This is similar to the base functionality you get from h2xs. It can
101 be very useful for binding to external libraries.
102
103 boot
104
105 Specifies C code to be executed in the XS BOOT section. Corresponds
106 to the XS parameter.
107
108 cc
109
110 Specify which compiler to use.
111
112 ccflags
113
114 Specify compiler flags - same as ExtUtils::MakeMaker's CCFLAGS
115 option. Whatever gets specified here replaces the default
116 $Config{ccflags}. Often, you'll want to add an extra flag or two
117 without clobbering the default flags in which case you could instead
118 use ccflagsex (see below) or, if Config.pm has already been loaded:
119
120 use Inline C => Config => ccflags => $Config{ccflags} . " -DXTRA -DTOO";
121
122 ccflagsex
123
124 Extend compiler flags. Sets CCFLAGS to $Config{ccflags} followed by a
125 space, followed by the specified value:
126
127 use Inline C => config => ccflagsex => "-DXTRA -DTOO";
128
129 cppflags
130
131 Specify preprocessor flags. Passed to cpp C preprocessor by
132 Preprocess() in Inline::Filters.
133
134 use Inline C => <<'END',
135 CPPFLAGS => ' -DPREPROCESSOR_DEFINE',
136 FILTERS => 'Preprocess';
137 use Inline C => <<'END',
138 CPPFLAGS => ' -DPREPROCESSOR_DEFINE=4321',
139 FILTERS => 'Preprocess';
140
141 filters
142
143 Allows you to specify a list of source code filters. If more than one
144 is requested, be sure to group them with an array ref. The filters
145 can either be subroutine references or names of filters provided by
146 the supplementary Inline::Filters module.
147
148 Your source code will be filtered just before it is parsed by Inline.
149 The MD5 fingerprint is generated before filtering. Source code
150 filters can be used to do things like stripping out POD
151 documentation, pre-expanding #include statements or whatever else you
152 please. For example:
153
154 use Inline C => DATA =>
155 filters => [Strip_POD => \&MyFilter => Preprocess ];
156
157 Filters are invoked in the order specified. See Inline::Filters for
158 more information.
159
160 If a filter is an array reference, it is assumed to be a usage of a
161 filter plug- in named by the first element of that array reference.
162 The rest of the elements of the array reference are used as arguments
163 to the filter. For example, consider a filters parameter like this:
164
165 use Inline C => DATA => filters => [ [ Ragel => '-G2' ] ];
166
167 In order for Inline::C to process this filter, it will attempt to
168 require the module Inline::Filters::Ragel and will then call the
169 filter function in that package with the argument '-G2'. This
170 function will return the actual filtering function.
171
172 inc
173
174 Specifies an include path to use. Corresponds to the MakeMaker
175 parameter. Expects a fully qualified path.
176
177 use Inline C => config => inc => '-I/inc/path';
178
179 ld
180
181 Specify which linker to use.
182
183 lddlflags
184
185 Specify which linker flags to use.
186
187 NOTE: These flags will completely override the existing flags,
188 instead of just adding to them. So if you need to use those too, you
189 must respecify them here.
190
191 libs
192
193 Specifies external libraries that should be linked into your code.
194 Corresponds to the MakeMaker parameter. Provide a fully qualified
195 path with the -L switch if the library is in a location where it
196 won't be found automatically.
197
198 use Inline C => config => libs => '-lyourlib';
199
200 or
201
202 use Inline C => config => libs => '-L/your/path -lyourlib';
203
204 make
205
206 Specify the name of the 'make' utility to use.
207
208 myextlib
209
210 Specifies a user compiled object that should be linked in.
211 Corresponds to the MakeMaker parameter. Expects a fully qualified
212 path.
213
214 use Inline C => config => myextlib => '/your/path/yourmodule.so';
215
216 optimize
217
218 This controls the MakeMaker OPTIMIZE setting. By setting this value
219 to '-g', you can turn on debugging support for your Inline
220 extensions. This will allow you to be able to set breakpoints in your
221 C code using a debugger like gdb.
222
223 prefix
224
225 Specifies a prefix that will be automatically stripped from C
226 functions when they are bound to Perl. Useful for creating wrappers
227 for shared library API-s, and binding to the original names in Perl.
228 Also useful when names conflict with Perl internals. Corresponds to
229 the XS parameter.
230
231 use Inline C => config => prefix => 'ZLIB_';
232
233 pre_head
234
235 Specifies code that will precede the inclusion of all files specified
236 in auto_include (ie EXTERN.h, perl.h, XSUB.h, INLINE.h and anything
237 else that might have been added to auto_include by the user). If the
238 specified value identifies a file, the contents of that file will be
239 inserted, otherwise the specified value is inserted.
240
241 use Inline C => config => pre_head => $code_or_filename;
242
243 prototype
244
245 Corresponds to the XS keyword 'PROTOTYPE'. See the perlxs
246 documentation for both 'PROTOTYPES' and 'PROTOTYPE'. As an example,
247 the following will set the PROTOTYPE of the 'foo' function to '$',
248 and disable prototyping for the 'bar' function.
249
250 use Inline C => config => prototype => {foo => '$', bar => 'DISABLE'}
251
252 prototypes
253
254 Corresponds to the XS keyword 'PROTOTYPES'. Can take only values of
255 'ENABLE' or 'DISABLE'. (Contrary to XS, default value is 'DISABLE').
256 See the perlxs documentation for both 'PROTOTYPES' and 'PROTOTYPE'.
257
258 use Inline C => config => prototypes => 'ENABLE';
259
260 typemaps
261
262 Specifies extra typemap files to use. These types will modify the
263 behaviour of the C parsing. Corresponds to the MakeMaker parameter.
264 Specify either a fully qualified path or a path relative to the cwd
265 (ie relative to what the cwd is at the time the script is loaded).
266
267 use Inline C => config => typemaps => '/your/path/typemap';
268
269 using
270
271 Specifies which parser to use. The default is
272 Inline::C::Parser::RecDescent, which uses the Parse::RecDescent
273 module.
274
275 The other options are ::Parser::Pegex and ::Parser::RegExp, which
276 uses the Inline::C::Parser::Pegex and Inline::C::Parser::RegExp
277 modules that ship with Inline::C.
278
279 use Inline C => config => using => '::Parser::Pegex';
280
281 Note that the following old options are deprecated, but still work at
282 this time:
283
284 * ParseRecDescent
285
286 * ParseRegExp
287
288 * ParsePegex
289
290C-PERL BINDINGS
291
292 This section describes how the Perl variables get mapped to C variables
293 and back again.
294
295 First, you need to know how Perl passes arguments back and forth to
296 subroutines. Basically it uses a stack (also known as the Stack). When
297 a sub is called, all of the parenthesized arguments get expanded into a
298 list of scalars and pushed onto the Stack. The subroutine then pops all
299 of its parameters off of the Stack. When the sub is done, it pushes all
300 of its return values back onto the Stack.
301
302 The Stack is an array of scalars known internally as SV's. The Stack is
303 actually an array of pointers to SV or SV*; therefore every element of
304 the Stack is natively a SV*. For FMTYEWTK about this, read perldoc
305 perlguts.
306
307 So back to variable mapping. XS uses a thing known as "typemaps" to
308 turn each SV* into a C type and back again. This is done through
309 various XS macro calls, casts and the Perl API. See perldoc perlapi. XS
310 allows you to define your own typemaps as well for fancier non-standard
311 types such as typedef- ed structs.
312
313 Inline uses the default Perl typemap file for its default types. This
314 file is called /usr/local/lib/perl5/5.6.1/ExtUtils/typemap, or
315 something similar, depending on your Perl installation. It has
316 definitions for over 40 types, which are automatically used by Inline.
317 (You should probably browse this file at least once, just to get an
318 idea of the possibilities.)
319
320 Inline parses your code for these types and generates the XS code to
321 map them. The most commonly used types are:
322
323 * int
324
325 * long
326
327 * double
328
329 * char*
330
331 * void
332
333 * SV*
334
335 If you need to deal with a type that is not in the defaults, just use
336 the generic SV* type in the function definition. Then inside your code,
337 do the mapping yourself. Alternatively, you can create your own typemap
338 files and specify them using the typemaps configuration option.
339
340 A return type of void has a special meaning to Inline. It means that
341 you plan to push the values back onto the Stack yourself. This is what
342 you need to do to return a list of values. If you really don't want to
343 return anything (the traditional meaning of void) then simply don't
344 push anything back.
345
346 If ellipsis or ... is used at the end of an argument list, it means
347 that any number of SV*s may follow. Again you will need to pop the
348 values off of the Stack yourself.
349
350 See "EXAMPLES" below.
351
352THE INLINE STACK MACROS
353
354 When you write Inline C, the following lines are automatically
355 prepended to your code (by default):
356
357 #include "EXTERN.h"
358 #include "perl.h"
359 #include "XSUB.h"
360 #include "INLINE.h"
361
362 The file INLINE.h defines a set of macros that are useful for handling
363 the Perl Stack from your C functions.
364
365 Inline_Stack_Vars
366
367 You'll need to use this one, if you want to use the others. It sets
368 up a few local variables: sp, items, ax and mark, for use by the
369 other macros. It's not important to know what they do, but I mention
370 them to avoid possible name conflicts.
371
372 NOTE: Since this macro declares variables, you'll need to put it with
373 your other variable declarations at the top of your function. It must
374 come before any executable statements and before any other
375 Inline_Stack macros.
376
377 Inline_Stack_Items
378
379 Returns the number of arguments passed in on the Stack.
380
381 Inline_Stack_Item(i)
382
383 Refers to a particular SV* in the Stack, where i is an index number
384 starting from zero. Can be used to get or set the value.
385
386 Inline_Stack_Reset
387
388 Use this before pushing anything back onto the Stack. It resets the
389 internal Stack pointer to the beginning of the Stack.
390
391 Inline_Stack_Push(sv)
392
393 Push a return value back onto the Stack. The value must be of type
394 SV*.
395
396 Inline_Stack_Done
397
398 After you have pushed all of your return values, you must call this
399 macro.
400
401 Inline_Stack_Return(n)
402
403 Return n items on the Stack.
404
405 Inline_Stack_Void
406
407 A special macro to indicate that you really don't want to return
408 anything. Same as:
409
410 Inline_Stack_Return(0);
411
412 Please note that this macro actually returns from your function.
413
414 Each of these macros is available in 3 different styles to suit your
415 coding tastes. The following macros are equivalent.
416
417 Inline_Stack_Vars
418 inline_stack_vars
419 INLINE_STACK_VARS
420
421 All of this functionality is available through XS macro calls as well.
422 So why duplicate the functionality? There are a few reasons why I
423 decided to offer this set of macros. First, as a convenient way to
424 access the Stack. Second, for consistent, self documenting, non-cryptic
425 coding. Third, for future compatibility. It occurred to me that if a
426 lot of people started using XS macros for their C code, the interface
427 might break under Perl6. By using this set, hopefully I will be able to
428 insure future compatibility of argument handling.
429
430 Of course, if you use the rest of the Perl API, your code will most
431 likely break under Perl6. So this is not a 100% guarantee. But since
432 argument handling is the most common interface you're likely to use, it
433 seemed like a wise thing to do.
434
435WRITING C SUBROUTINES
436
437 The definitions of your C functions will fall into one of the following
438 four categories. For each category there are special considerations.
439
440 int Foo(int arg1, char* arg2, SV* arg3) {
441
442 This is the simplest case. You have a non void return type and a
443 fixed length argument list. You don't need to worry about much. All
444 the conversions will happen automatically.
445
446 void Foo(int arg1, char* arg2, SV* arg3) {
447
448 In this category you have a void return type. This means that either
449 you want to return nothing, or that you want to return a list. In the
450 latter case you'll need to push values onto the Stack yourself. There
451 are a few Inline macros that make this easy. Code something like
452 this:
453
454 int i, max; SV* my_sv[10];
455 Inline_Stack_Vars;
456 Inline_Stack_Reset;
457 for (i = 0; i < max; i++)
458 Inline_Stack_Push(my_sv[i]);
459 Inline_Stack_Done;
460
461 After resetting the Stack pointer, this code pushes a series of
462 return values. At the end it uses Inline_Stack_Done to mark the end
463 of the return stack.
464
465 If you really want to return nothing, then don't use the
466 Inline_Stack_ macros. If you must use them, then set use
467 Inline_Stack_Void at the end of your function.
468
469 char* Foo(SV* arg1, ...) {
470
471 In this category you have an unfixed number of arguments. This means
472 that you'll have to pop values off the Stack yourself. Do it like
473 this:
474
475 int i;
476 Inline_Stack_Vars;
477 for (i = 0; i < Inline_Stack_Items; i++)
478 handle_sv(Inline_Stack_Item(i));
479
480 The return type of Inline_Stack_Item(i) is SV*.
481
482 void* Foo(SV* arg1, ...) {
483
484 In this category you have both a void return type and an unfixed
485 number of arguments. Just combine the techniques from Categories 3
486 and 4.
487
488EXAMPLES
489
490 Here are a few examples. Each one is a complete program that you can
491 try running yourself. For many more examples see Inline::C::Cookbook.
492
493 Example #1 - Greetings
494
495 This example will take one string argument (a name) and print a
496 greeting. The function is called with a string and with a number. In
497 the second case the number is forced to a string.
498
499 Notice that you do not need to #include <stdio.h>. The perl.h header
500 file which gets included by default, automatically loads the standard C
501 header files for you.
502
503 use Inline 'C';
504 greet('Ingy');
505 greet(42);
506 __END__
507 __C__
508 void greet(char* name) {
509 printf("Hello %s!\n", name);
510 }
511
512 Example #2 - and Salutations
513
514 This is similar to the last example except that the name is passed in
515 as a SV* (pointer to Scalar Value) rather than a string (char*). That
516 means we need to convert the SV to a string ourselves. This is
517 accomplished using the SvPVX function which is part of the Perl
518 internal API. See perldoc perlapi for more info.
519
520 One problem is that SvPVX doesn't automatically convert strings to
521 numbers, so we get a little surprise when we try to greet 42. The
522 program segfaults, a common occurrence when delving into the guts of
523 Perl.
524
525 use Inline 'C';
526 greet('Ingy');
527 greet(42);
528 __END__
529 __C__
530 void greet(SV* sv_name) {
531 printf("Hello %s!\n", SvPVX(sv_name));
532 }
533
534 Example #3 - Fixing the problem
535
536 We can fix the problem in Example #2 by using the SvPV function
537 instead. This function will stringify the SV if it does not contain a
538 string. SvPV returns the length of the string as it's second parameter.
539 Since we don't care about the length, we can just put PL_na there,
540 which is a special variable designed for that purpose.
541
542 use Inline 'C';
543 greet('Ingy');
544 greet(42);
545 __END__
546 __C__
547 void greet(SV* sv_name) {
548 printf("Hello %s!\n", SvPV(sv_name, PL_na));
549 }
550
551SEE ALSO
552
553 For general information about Inline see Inline.
554
555 For sample programs using Inline with C see Inline::C::Cookbook.
556
557 For information on supported languages and platforms see
558 Inline-Support.
559
560 For information on writing your own Inline Language Support Module, see
561 Inline-API.
562
563 Inline's mailing list is inline@perl.org
564
565 To subscribe, send email to inline-subscribe@perl.org
566
567BUGS AND DEFICIENCIES
568
569 If you use C function names that happen to be used internally by Perl,
570 you will get a load error at run time. There is currently no
571 functionality to prevent this or to warn you. For now, a list of Perl's
572 internal symbols is packaged in the Inline module distribution under
573 the filename 'symbols.perl'. Avoid using these in your code.
574
575AUTHORS
576
577 Ingy döt Net <ingy@cpan.org>
578
579 Sisyphus <sisyphus@cpan.org>
580
581COPYRIGHT AND LICENSE
582
583 Copyright 2000-2019. Ingy döt Net.
584
585 Copyright 2008, 2010-2014. Sisyphus.
586
587 This program is free software; you can redistribute it and/or modify it
588 under the same terms as Perl itself.
589
590 See http://www.perl.com/perl/misc/Artistic.html
591
592