SWIG
Encyclopedia
SWIG is an open source software tool used to connect computer program
s or libraries written in C
or C++
with scripting language
s such as Lua, Perl
, PHP
, Python
, R
, Ruby
, Tcl
, and other languages like C#, Java
, Modula-3
, Objective Caml
, Octave
, and Scheme. Output can also be in the form of XML
or Lisp S-expression
s.
for functions with simple arguments; conversion code for complex types of arguments has to be written by programmer. SWIG tool creates source code
which provides the glue between C/C++ and the target language. Depending on the language, this glue comes in two forms:
SWIG is not used for calling interpreted functions by native code; this has to be done by the programmer manually.
in an extant C/C++ program:
There are several reasons to create dynamic libraries that can be loaded into extant interpreters, including:
and the University of Utah
and while on the faculty at the University of Chicago
. Development is currently supported by an active group of volunteers led by William Fulton. SWIG has been released under a GNU General Public License
.
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
s or libraries written in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
or C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
with scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
s such as Lua, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
, PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
, R
R (programming language)
R is a programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software, and R is widely used for statistical software development and data analysis....
, Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
, Tcl
Tcl
Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration", according to the author, with programmers devising their own languages intended to be embedded into applications, Tcl gained acceptance on its own...
, and other languages like C#, Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
, Modula-3
Modula-3
In computer science, Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2 known as Modula-2+. While it has been influential in research circles it has not been adopted widely in industry...
, Objective Caml
Objective Caml
OCaml , originally known as Objective Caml, is the main implementation of the Caml programming language, created by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy and others in 1996...
, Octave
GNU Octave
GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command-line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with MATLAB...
, and Scheme. Output can also be in the form of XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
or Lisp S-expression
S-expression
S-expressions or sexps are list-based data structures that represent semi-structured data. An S-expression may be a nested list of smaller S-expressions. S-expressions are probably best known for their use in the Lisp family of programming languages...
s.
Function
The aim is to allow calling native functions (C or C++) by interpreted code, passing complex data types, keeping memory from being freed, inheriting classes across languages etc. The programmer writes an interface file containing a list of C/C++ functions to be visible to an interpreter. SWIG will compile the interface file into a regular C/C++. SWIG will generate conversion codeMarshalling (computer science)
In computer science, marshalling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission...
for functions with simple arguments; conversion code for complex types of arguments has to be written by programmer. SWIG tool creates source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...
which provides the glue between C/C++ and the target language. Depending on the language, this glue comes in two forms:
- a shared library that an extant interpreter can link to as some form of extension module, or
- a shared library that can be linked to other programs compiled in the target language (for example, using JNIJava Native InterfaceThe Java Native Interface is a programming framework that enables Java code running in a Java Virtual Machine to call and to be called by native applications and libraries written in other languages such as C, C++ and assembly.-Purpose and features:JNI enables one to write native methods to...
in Java).
SWIG is not used for calling interpreted functions by native code; this has to be done by the programmer manually.
Purpose
There are two main reasons to embed a scripting engineScripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
in an extant C/C++ program:
- The program can then be customized far faster, via a scripting language instead of C/C++Ousterhout's dichotomyOusterhout's dichotomy is computer scientist John Ousterhout's claim that high-level programming languages tend to fall into two groups, each with distinct properties and uses: "system programming languages" and "scripting languages"...
. The scripting engine may even be exposed to the end user, so that they can automate common tasks by writing scripts. - Even if the final product is not to contain the scripting engine, it may nevertheless be very useful for writing test scripts.
There are several reasons to create dynamic libraries that can be loaded into extant interpreters, including:
- Provide access to a C/C++ libraryLibrary (computer science)In computer science, a library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications....
which has no equivalent in the scripting language. - Write the whole program in the scripting language first, and after profilingPerformance analysisIn software engineering, profiling is a form of dynamic program analysis that measures, for example, the usage of memory, the usage of particular instructions, or frequency and duration of function calls...
, rewrite performance critical code in C or C++. - Unit testing of C or C++ code is often better done from a scripting language.
History
SWIG is written in C and C++ and has been publicly available since February 1996. The initial author and main developer was Dave Beazley who developed SWIG while working as a graduate student at Los Alamos National LaboratoryLos Alamos National Laboratory
Los Alamos National Laboratory is a United States Department of Energy national laboratory, managed and operated by Los Alamos National Security , located in Los Alamos, New Mexico...
and the University of Utah
University of Utah
The University of Utah, also known as the U or the U of U, is a public, coeducational research university in Salt Lake City, Utah, United States. The university was established in 1850 as the University of Deseret by the General Assembly of the provisional State of Deseret, making it Utah's oldest...
and while on the faculty at the University of Chicago
University of Chicago
The University of Chicago is a private research university in Chicago, Illinois, USA. It was founded by the American Baptist Education Society with a donation from oil magnate and philanthropist John D. Rockefeller and incorporated in 1890...
. Development is currently supported by an active group of volunteers led by William Fulton. SWIG has been released under a GNU General Public License
GNU General Public License
The GNU General Public License is the most widely used free software license, originally written by Richard Stallman for the GNU Project....
.
See also
- Language bindingLanguage bindingIn computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...
- Foreign function interfaceForeign function interfaceA foreign function interface is a mechanism by which a program written in one programming language can call routines or make use of services written in another. The term comes from the specification for Common Lisp, which explicitly refers to the language features for inter-language calls as...
- Calling conventionCalling conventionIn computer science, a calling convention is a scheme for how subroutines receive parameters from their caller and how they return a result; calling conventions can differ in:...
- Name manglingName manglingIn compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....
- Application programming interfaceApplication programming interfaceAn application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...
- API - Application Binary InterfaceApplication binary interfaceIn computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...
- ABI - Comparison of application virtual machinesComparison of Application Virtual MachinesThis article lists some software virtual machines that are typically used for allowing application bytecode to be portably run on many different computer architectures and operating systems. The application is usually run on the computer using an interpreter or just-in-time compilation...
- Wrapper functionWrapper functionA wrapper function is a function in a computer program whose main purpose is to call a second function with little or no additional computation. This is also known as method delegation. Wrapper functions can be used for a number of purposes....
Projects Using SWIG
- ZXIDZXIDZXID.org Identity Management toolkit implements standalone SAML 2.0,Liberty ID-WSF 2.0, and XACML 2.0 stacks and aims at implementing all popularfederation, SSO, and ID Web Services protocols. It is a C implementation...
(Apache2 open source) - Symlabs SFIS (commercial)
- etc. Many other projects use SWIG. Please add them here.
External links
- Project home page at SourceForgeSourceForgeSourceForge Enterprise Edition is a collaborative revision control and software development management system. It provides a front-end to a range of software development lifecycle services and integrates with a number of free software / open source software applications .While originally itself...
- SwigWiki, WikiWikiA wiki is a website that allows the creation and editing of any number of interlinked web pages via a web browser using a simplified markup language or a WYSIWYG text editor. Wikis are typically powered by wiki software and are often used collaboratively by multiple users. Examples include...
with information on using SWIG - Article "Expose Your C/C++ Program's Internal API with a Quick SWIG" by Victor Volkman
- Article "Python Extensions In C++ Using SWIG" by Michael Fötsch
- Presentation "Application overview for openSUSE" by Klaus Kämpf
- Citations from CiteSeer