ArticleslgStudy

computer science

SWIG

SWIG is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand SWIG rather than just read about it. In short: The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other language implementations like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme. Output can also be in the form of XML.

SWIG — main illustration
SWIG — illustration

Key takeaways

  • SWIG belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect SWIG to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of SWIG from memory before moving on to harder problems.

Reference excerpt

The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other language implementations like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme. Output can also be in the form of XML.

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 that provides the glue between C/C++ and the target language. Depending on the language, this glue comes in three 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). a shared dynamic library source code that should be compiled and dynamically loaded (e.g. Node.js native extensions) 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:

In this file, there are two functions sin() and strcmp(), a global variable Foo, and two constants STATUS and VERSION. When SWIG creates an extension module, these declarations are accessible as scripting language functions, variables, and constants respectively. In Python:

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 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 who developed SWIG while working as a graduate student at Los Alamos National Laboratory 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.

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 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, Pybind11, and Boost's Boost.python library.

Projects using SWIG ZXID (Apache License, Version 2.0) Symlabs SFIS (commercial) LLDB GNU Radio up to (including) version 3.8.x.x; later versions use Pybind11 Xapian TensorFlow Apache SINGA QuantLib Babeltrace

See also

Language binding Foreign function interface (FFI) Calling convention Name mangling Application programming interface Application binary interface Comparison of application virtual machines Wrapper function 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 "Python Extensions In C++ Using SWIG" by Michael Fötsch Presentation "Application overview for openSUSE" by Klaus Kämpf

External links Official website

Worked examples

Example 1 — a first encounter with SWIG

Start with the simplest possible case. Write down what SWIG claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to SWIG before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about SWIG ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of SWIG

In research
SWIG appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses SWIG in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
SWIG is common in secondary-school and first-year university syllabi. It links to neighbouring topics Cross-platform software, Free computer programming tools, Programming tools, so understanding it makes those chapters shorter.
In everyday life
Look for SWIG outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “SWIG” →

How to study SWIG in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what SWIG means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain SWIG out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is SWIG in simple terms?

The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other language implementations like C#, Java, JavaScript, Go, D, OCaml…

Why does SWIG matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study SWIG?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on SWIG.

Tags

  • Cross-platform software
  • Free computer programming tools
  • Programming tools
  • Scripting languages

Keep exploring