The Simplified Wrapper and Interface Generator (SWIG) is an
open-source software
Open-source software (OSS) is computer software that is released under a license in which the copyright holder grants users the rights to use, study, change, and distribute the software and its source code to anyone and for any purpose. Ope ...
tool used to connect
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components.
A computer progra ...
s or
libraries
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
written in
C or
C++ with
scripting language
A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.
A scripti ...
s such as
Lua
Lua or LUA may refer to:
Science and technology
* Lua (programming language)
* Latvia University of Agriculture
* Last universal ancestor, in evolution
Ethnicity and language
* Lua people, of Laos
* Lawa people, of Thailand sometimes referred t ...
,
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
,
PHP
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
,
Python,
R,
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
,
Tcl, and other languages like
C#,
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
,
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
,
Go,
D,
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, D ...
,
Octave
In music, an octave ( la, octavus: eighth) or perfect octave (sometimes called the diapason) is the interval between one musical pitch and another with double its frequency. The octave relationship is a natural phenomenon that has been refer ...
,
Scilab and
Scheme. Output can also be in the form of
XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. ...
.
Function
The aim is to allow the calling of native functions (that were written in C or C++) by other programming languages, passing complex data types to those functions, keeping memory from being inappropriately freed, inheriting object classes across languages, etc. The programmer writes an interface file containing a list of C/C++ functions to be made visible to an interpreter. SWIG will compile the interface file and generate code in regular C/C++ and the target programming language. SWIG will generate
conversion code for functions with simple arguments; conversion code for complex types of arguments must be written by the programmer. The SWIG tool creates
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
that 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
Java Native Interface (JNI) in Java).
SWIG is not used for calling interpreted functions by native code; this must be done by the programmer manually.
Example
SWIG wraps simple C declarations by creating an interface that closely matches the way in which the declarations would be used in a C program. For example, consider the following interface file:
%module example
%inline %
#define STATUS 50
#define VERSION "1.1"
In this file, there are two functions and , a global variable , and two constants and . When SWIG creates an extension module, these declarations are accessible as scripting language functions, variables, and constants respectively. In Python:
>>> example.sin(3)
0.141120008
>>> example.strcmp('Dave','Mike')
-1
>>> print(example.cvar.Foo)
42
>>> print(example.STATUS)
50
>>> print(example.VERSION)
1.1
Purpose
There are two main reasons to embed a
scripting engine in an existing C/C++ program:
*The program can then be customized far faster,
via a scripting language instead of C/C++. 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++
library
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vi ...
which has no equivalent in the scripting language.
*Write the whole program in the scripting language first, and after
profiling, rewrite performance-critical code in C or C++.
History
SWIG is written in C and C++ and has been publicly available since February 1996. The initial author and main developer was
David M. Beazley
David Beazley is an American software engineer. He has made significant contributions to the Python developer community, which includes writing the definitive Python reference text ''Python Essential Reference'', the SWIG software tool for creati ...
who developed SWIG while working as a graduate student at
Los Alamos National Laboratory
Los Alamos National Laboratory (often shortened as Los Alamos and LANL) is one of the sixteen research and development laboratories of the United States Department of Energy (DOE), located a short distance northwest of Santa Fe, New Mexico, i ...
and the
University of Utah
The University of Utah (U of U, UofU, or simply The U) is a public research university in Salt Lake City, Utah. It is the flagship institution of the Utah System of Higher Education. The university was established in 1850 as the University of D ...
and while on the faculty at the
University of Chicago
The University of Chicago (UChicago, Chicago, U of C, or UChi) is a private university, private research university in Chicago, Illinois. Its main campus is located in Chicago's Hyde Park, Chicago, Hyde Park neighborhood. The University of Chic ...
. Development is currently supported by an active group of volunteers led by William Fulton. SWIG has been released under a
GNU General Public License
The GNU General Public License (GNU GPL or simply GPL) is a series of widely used free software licenses that guarantee end user
In product development, an end user (sometimes end-user) is a person who ultimately uses or is intended to ulti ...
.
Google Summer of Code
SWIG was a successful participant of
Google Summer of Code in 2008, 2009, 2012. In 2008, SWIG got four slots. Haoyu Bai spent his summers on SWIG's Python 3.0 Backend, Jan Jezabek worked on Support for generating
COM wrappers, Cheryl Foil spent her time on Comment 'Translator' for SWIG, and Maciej Drwal worked on a C backend.
In 2009, SWIG again participated in Google Summer of Code. This time four students participated. Baozeng Ding worked on a
Scilab module. Matevz Jekovec spent time on
C++0x features. Ashish Sharma spent his summer on an
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
module, Miklos Vajna spent his time on PHP directors.
In 2012, SWIG participated in Google Summer of Code. This time four out of five students successfully completed the project. Leif Middelschulte worked on a C target language module. Swati Sharma enhanced the Objective-C module. Neha Narang added the new module on JavaScript. Dmitry Kabak worked on source code documentation and
Doxygen comments.
Alternatives
For Python, similar functionality is offered by
SIP and
Boost's Boost.python library.
Projects Using SWIG
*
ZXID (Apache License, Version 2.0)
*Symlabs SFIS (commercial)
*
LLDB
*
GNU Radio
*
Xapian
*
TensorFlow
*
Apache SINGA
Apache SINGA is an Apache top-level project for developing an open source machine learning library. It provides a flexible architecture for scalable distributed training, is extensible to run over a wide range of hardware, and has a focus on hea ...
*
QuantLib
QuantLib is an open-source software library which provides tools for software developers and practitioners interested in financial instrument valuation and related subjects. QuantLib is written in C++.
History
The QuantLib project was started by ...
*
Babeltrace
LTTng (Linux Trace Toolkit: next generation) is a system software package for correlated tracing of the Linux kernel, applications and libraries. The project was originated by Mathieu Desnoyers with an initial release in 2005. Its predecessor is t ...
See also
*
Language binding
*
Foreign function interface
*
Calling convention
In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines or functions receive parameters from their caller and how they return a result. When some code calls a function, design choices have b ...
*
Name mangling
*
Application programming interface
*
Application binary interface
In computer software, an application binary interface (ABI) is an interface between two binary program modules. Often, one of these modules is a library or operating system facility, and the other is a program that is being run by a user.
An ...
*
Comparison of application virtual machines
*
Wrapper function
A wrapper function is a function (another word for a ''subroutine'') in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation. Wrapper functions are u ...
*
GIWS for the opposite of SWIG: calling Java from C/C++
References
Further reading
*Article
Expose Your C/C++ Program's Internal API with a Quick SWIG by Victor Volkman
*Article
by Michael Fötsch
*Presentation
Application overview for openSUSE by Klaus Kämpf
External links
*{{Official website
Cross-platform software
Free computer programming tools
Programming tools
Scripting languages