SWIG Perl Examples
The following examples illustrate the use of SWIG with Perl.
- simple. A minimal example showing how SWIG can
be used to wrap a C function and a global variable.
- constants. This shows how preprocessor macros and
certain C declarations are turned into constants.
- variables. This example shows how to access C global variables from Perl.
- value. How to pass and return structures by value.
- class. How to wrap a simple C++ class.
- reference. C++ references.
- pointer. Simple pointer handling.
- funcptr. Pointers to functions.
- callback. C++ callbacks using directors.
- extend. Extending a simple C++ class.
Compilation Issues
- To create a Perl extension, SWIG is run with the following options:
% swig -perl5 interface.i
- The compilation of examples is done using the file Example/Makefile. This
makefile performs a manual module compilation which is platform specific. Typically,
the steps look like this (Linux):
% swig -perl5 interface.i
% gcc -fpic -c -Dbool=char -I/usr/lib/perl5/5.00503/i386-linux/CORE interface_wrap.c
% gcc -shared interface_wrap.o $(OBJS) -o interface.so
% perl
use interface;
...
- The politically "correct" way to compile a Perl extension module is to use MakeMaker
and related tools (especially if you are considering third-party distribution). Consult
a book such as Advanced Perl Programming for details.
Compatibility
The examples have been extensively tested on the following platforms:
Please see the Windows page in the main manual for information on using the examples on Windows.
Due to wide variations in the Perl C API and differences between versions such as the ActivePerl release for Windows,
the code generated by SWIG is extremely messy.
If the code doesn't compile or work with your version of Perl, please let us know by
contacting us on the mailing lists.
Better yet, send us a patch.