=pod
=for comment DO NOT EDIT. This Pod was generated by Swim v0.1.48. See http://github.com/ingydotnet/swim-pm#readme
=encoding utf8
=head1 Name
Inline::C - C Language Support for Inline
=head1 Version
This document describes L
=head1 Description
C
If you want to start working with programming examples right away, check out
L
=head1 Usage
You never actually use C
use Inline C => ...;
or
bind Inline C => ...;
=head1 Function Definitions
The Inline grammar for C recognizes certain function definitions (or
signatures) in your C code. If a signature is recognized by Inline, then it
will be available in Perl-space. That is, Inline will generate the "glue"
necessary to call that function as if it were a Perl subroutine. If the
signature is not recognized, Inline will simply ignore it, with no complaints.
It will not be available from Perl-space, although it I
Inline looks for ANSI/prototype style function definitions. They must be of the form:
return-type function-name ( type-name-pairs ) { ... }
The most common types are: C
A return type of C
int Foo(double num, char* str) {
void Foo(double num, char* str) {
void Foo(SV*, ...) {
long Foo(int i, int j, ...) {
SV* Foo(void) { # 'void' arg invalid with the ParseRecDescent parser.
# Works only with the ParseRegExp parser.
# See the section on `using` (below).
SV* Foo() { # Alternative to specifying 'void' arg. Is valid with
# both the ParseRecDescent and ParseRegExp parsers.
The following definitions would not be recognized:
Foo(int i) { # no return type
int Foo(float f) { # no (default) typemap for float
int Foo(num, str) double num; char* str; {
Notice that Inline only looks for function I
=head1 C Configuration Options
For information on how to specify Inline configuration options, see L
=over
=item C
Specifies extra statements to automatically included. They will be added onto the defaults. A newline char will be automatically added.
use Inline C => config => auto_include => '#include "yourheader.h"';
=item C
If you C<< enable => autowrap >>, Inline::C will parse function declarations (prototype statements) in your C code. For each declaration it can bind to, it will create a dummy wrapper that will call the real function which may be in an external library. This is a nice convenience for functions that would otherwise just require an empty wrapper function.
This is similar to the base functionality you get from C
=item C
Specifies C code to be executed in the XS C
=item C
Specify which compiler to use.
=item C
Specify compiler flags - same as ExtUtils::MakeMaker's C
use Inline C => Config => ccflags => $Config{ccflags} . " -DXTRA -DTOO";
=item C
Extend compiler flags. Sets C
use Inline C => config => ccflagsex => "-DXTRA -DTOO";
=item C
=back
Specify preprocessor flags. Passed to C
use Inline C => <<'END',
CPPFLAGS => ' -DPREPROCESSOR_DEFINE',
FILTERS => 'Preprocess';
use Inline C => <<'END',
CPPFLAGS => ' -DPREPROCESSOR_DEFINE=4321',
FILTERS => 'Preprocess';
=over
=item C
Allows you to specify a list of source code filters. If more than one is requested, be sure to group them with an array ref. The filters can either be subroutine references or names of filters provided by the supplementary Inline::Filters module.
Your source code will be filtered just before it is parsed by Inline. The MD5 fingerprint is generated before filtering. Source code filters can be used to do things like stripping out POD documentation, pre-expanding C<#include> statements or whatever else you please. For example:
use Inline C => DATA =>
filters => [Strip_POD => \&MyFilter => Preprocess ];
Filters are invoked in the order specified. See L
If a filter is an array reference, it is assumed to be a usage of a filter plug-
in named by the first element of that array reference. The rest of the
elements of the array reference are used as arguments to the filter. For
example, consider a C
use Inline C => DATA => filters => [ [ Ragel => '-G2' ] ];
In order for Inline::C to process this filter, it will attempt to require the
module L
=item C
Specifies an include path to use. Corresponds to the MakeMaker parameter. Expects a fully qualified path.
use Inline C => config => inc => '-I/inc/path';
=item C
Specify which linker to use.
=item C
Specify which linker flags to use.
NOTE: These flags will completely override the existing flags, instead of just adding to them. So if you need to use those too, you must respecify them here.
=item C
Specifies external libraries that should be linked into your code. Corresponds to the MakeMaker parameter. Provide a fully qualified path with the C<-L> switch if the library is in a location where it won't be found automatically.
use Inline C => config => libs => '-lyourlib';
or
use Inline C => config => libs => '-L/your/path -lyourlib';
=item C
Specify the name of the 'make' utility to use.
=item C
Specifies a user compiled object that should be linked in. Corresponds to the MakeMaker parameter. Expects a fully qualified path.
use Inline C => config => myextlib => '/your/path/yourmodule.so';
=item C
This controls the MakeMaker C
=item C
Specifies a prefix that will be automatically stripped from C functions when they are bound to Perl. Useful for creating wrappers for shared library API-s, and binding to the original names in Perl. Also useful when names conflict with Perl internals. Corresponds to the XS parameter.
use Inline C => config => prefix => 'ZLIB_';
=item C
Specifies code that will precede the inclusion of all files specified in
C
use Inline C => config => pre_head => $code_or_filename;
=item C
Corresponds to the XS keyword 'PROTOTYPE'. See the perlxs documentation for both 'PROTOTYPES' and 'PROTOTYPE'. As an example, the following will set the PROTOTYPE of the 'foo' function to '$', and disable prototyping for the 'bar' function.
use Inline C => config => prototype => {foo => '$', bar => 'DISABLE'}
=item C
Corresponds to the XS keyword 'PROTOTYPES'. Can take only values of 'ENABLE' or 'DISABLE'. (Contrary to XS, default value is 'DISABLE'). See the perlxs documentation for both 'PROTOTYPES' and 'PROTOTYPE'.
use Inline C => config => prototypes => 'ENABLE';
=item C
Specifies extra typemap files to use. These types will modify the behaviour of the C parsing. Corresponds to the MakeMaker parameter. Specify either a fully qualified path or a path relative to the cwd (ie relative to what the cwd is at the time the script is loaded).
use Inline C => config => typemaps => '/your/path/typemap';
=item C
Specifies which parser to use. The default is
L
The other options are C<::Parser::Pegex> and C<::Parser::RegExp>, which uses
the L
use Inline C => config => using => '::Parser::Pegex';
Note that the following old options are deprecated, but still work at this time:
=over
=item * C
=item * C
=item * C
=back
=back
=head1 C-Perl Bindings
This section describes how the C
First, you need to know how C
The B
So back to variable mapping. XS uses a thing known as "typemaps" to turn each
C<SV*> into a C
Inline uses the default Perl typemap file for its default types. This file is called C</usr/local/lib/perl5/5.6.1/ExtUtils/typemap>, or something similar, depending on your Perl installation. It has definitions for over 40 types, which are automatically used by Inline. (You should probably browse this file at least once, just to get an idea of the possibilities.)
Inline parses your code for these types and generates the XS code to map them. The most commonly used types are:
=over
=item * C
=item * C
=item * C
=item C<char>
=item * C
=item C<SV>
=back
If you need to deal with a type that is not in the defaults, just use the
generic C<SV*> type in the function definition. Then inside your code, do the
mapping yourself. Alternatively, you can create your own typemap files and
specify them using the C
A return type of C
If ellipsis or C<...> is used at the end of an argument list, it means that
any number of C<SV*>s may follow. Again you will need to pop the values off of
the C
See L<"EXAMPLES"> below.
=head1 The Inline Stack Macros
When you write Inline C, the following lines are automatically prepended to your code (by default):
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "INLINE.h"
The file C
=over
=item C
You'll need to use this one, if you want to use the others. It sets up a few
local variables: C
NOTE: Since this macro declares variables, you'll need to put it with your
other variable declarations at the top of your function. It must
come before any executable statements and before any other
C
=item C
Returns the number of arguments passed in on the Stack.
=item C<Inline_Stack_Item(i)>
Refers to a particular C<SV*> in the Stack, where C is an index number starting from zero. Can be used to get or set the value.
=item C
Use this before pushing anything back onto the Stack. It resets the internal Stack pointer to the beginning of the Stack.
=item C<Inline_Stack_Push(sv)>
Push a return value back onto the Stack. The value must be of type C<SV*>.
=item C
After you have pushed all of your return values, you must call this macro.
=item C<Inline_Stack_Return(n)>
Return C
=item C
A special macro to indicate that you really don't want to return anything. Same as:
Inline_Stack_Return(0);
Please note that this macro actually B
=back
Each of these macros is available in 3 different styles to suit your coding tastes. The following macros are equivalent.
Inline_Stack_Vars
inline_stack_vars
INLINE_STACK_VARS
All of this functionality is available through XS macro calls as well. So why duplicate the functionality? There are a few reasons why I decided to offer this set of macros. First, as a convenient way to access the Stack. Second, for consistent, self documenting, non-cryptic coding. Third, for future compatibility. It occurred to me that if a lot of people started using XS macros for their C code, the interface might break under Perl6. By using this set, hopefully I will be able to insure future compatibility of argument handling.
Of course, if you use the rest of the Perl API, your code will most likely break under Perl6. So this is not a 100% guarantee. But since argument handling is the most common interface you're likely to use, it seemed like a wise thing to do.
=head1 Writing C Subroutines
The definitions of your C functions will fall into one of the following four categories. For each category there are special considerations.
=over
=item C<int Foo(int arg1, char arg2, SV arg3) {>
This is the simplest case. You have a non C
=item C<void Foo(int arg1, char arg2, SV arg3) {>
In this category you have a C
int i, max; SV* my_sv[10];
Inline_Stack_Vars;
Inline_Stack_Reset;
for (i = 0; i < max; i++)
Inline_Stack_Push(my_sv[i]);
Inline_Stack_Done;
After resetting the Stack pointer, this code pushes a series of return values.
At the end it uses C
If you really want to return nothing, then don't use the C
=item C<char Foo(SV arg1, ...) {>
In this category you have an unfixed number of arguments. This means that
you'll have to pop values off the B
int i;
Inline_Stack_Vars;
for (i = 0; i < Inline_Stack_Items; i++)
handle_sv(Inline_Stack_Item(i));
The return type of C<Inline_Stack_Item(i)> is C<SV*>.
=item C<void Foo(SV arg1, ...) {>
In this category you have both a C
=back
=head1 Examples
Here are a few examples. Each one is a complete program that you can try
running yourself. For many more examples see L
=head2 Example #1 - Greetings
This example will take one string argument (a name) and print a greeting. The function is called with a string and with a number. In the second case the number is forced to a string.
Notice that you do not need to C<< #include
use Inline 'C';
greet('Ingy');
greet(42);
__END__
__C__
void greet(char* name) {
printf("Hello %s!\n", name);
}
=head2 Example #2 - and Salutations
This is similar to the last example except that the name is passed in as a
C<SV> (pointer to Scalar Value) rather than a string (C<char>). That means
we need to convert the C
One problem is that C
use Inline 'C';
greet('Ingy');
greet(42);
__END__
__C__
void greet(SV* sv_name) {
printf("Hello %s!\n", SvPVX(sv_name));
}
=head2 Example #3 - Fixing the problem
We can fix the problem in Example #2 by using the C
use Inline 'C';
greet('Ingy');
greet(42);
__END__
__C__
void greet(SV* sv_name) {
printf("Hello %s!\n", SvPV(sv_name, PL_na));
}
=head1 See Also
For general information about Inline see L
For sample programs using Inline with C see L
For information on supported languages and platforms see L
For information on writing your own Inline Language Support Module, see
L
Inline's mailing list is inline@perl.org
To subscribe, send email to inline-subscribe@perl.org
=head1 Bugs and Deficiencies
If you use C function names that happen to be used internally by Perl, you will get a load error at run time. There is currently no functionality to prevent this or to warn you. For now, a list of Perl's internal symbols is packaged in the Inline module distribution under the filename C<'symbols.perl'>. Avoid using these in your code.
=head1 Authors
Ingy döt Net ingy@cpan.org
Sisyphus sisyphus@cpan.org
=head1 Copyright and License
Copyright 2000-2022. Ingy döt Net.
Copyright 2008, 2010-2014. Sisyphus.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See Lhttp://www.perl.com/perl/misc/Artistic.html
=cut