Chapter 39. SWIG
Scott Bolte
Tip
When this article was written, the Inline module discussed in Using Other Languages from Perl didn’t exist. When you want to use C from Perl, you should consider Inline, SWIG, and XS, in that order.
Tired of the “which interpreter is best for the job” debates? Want something that makes the speed and power of C and C++ available to several common interpreters automatically? There’s a new tool that does exactly that: SWIG, the Simplified Wrapper and Interface Generator.
SWIG is a freely available compiler developed by Dave Beazley, now at the University of Chicago. It works on many Unix variants, as well as Windows and Mac OS, although the Windows versions are less polished. It converts C and C++ files into interface code (called a wrapper) that makes the contents available to Perl, Tcl, Guile, Java, Ruby, and Python—all automatically. Like Perl’s own XS language, SWIG creates Perl wrappers around C code. Unlike XS, SWIG requires little or no programming effort.
I’m not going to describe the mechanics of SWIG in detail; a more thorough treatment would address function prototypes, complex data structures, multiple inheritance, Perl classes, Tcl 8 modules, exception handling, Makefile generation, strict type checking in the interpreter, conditional compilation, and automatic generation of documentation. There’s an excellent user guide bundled with SWIG that covers all of these topics. In this article, I’ll just show you the basics: how to take an existing ...
Get Computer Science & Perl Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.