Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
SWIG
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Open-source programming tool}} {{Other uses|Swig (disambiguation){{!}}Swig}} {{Multiple issues| {{more footnotes|date=August 2017}} {{more citations needed|date=August 2017}} }} {{Infobox software | name = SWIG | author = [[David M. Beazley]] | developer = SWIG developers | released = {{Start date and age|1996|02}}<ref>{{Cite web|url=https://www.swig.org/history.html|title = SWIG History}}</ref> | latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}} | latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}} | programming language = [[C (programming language)|C]], [[C++]] | license = [[GNU General Public License|GPLv3]] | website = {{URL|swig.org}} }} The '''Simplified Wrapper and Interface Generator''' ('''SWIG''') is an [[open-source software]] tool used to connect [[computer program]]s or [[library (computing)|libraries]] written in [[C (programming language)|C]] or [[C++]] with [[scripting language]]s such as [[Lua (programming language)|Lua]], [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]], [[Tcl]], and other language implementations like [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]], [[JavaScript]], [[Go (programming language)|Go]], [[D (programming language)|D]], [[OCaml]], [[GNU Octave|Octave]], [[Scilab]] and [[Scheme (programming language)|Scheme]]. Output can also be in the form of [[XML]]. ==Function== [[File:Swig file interrelations.svg|400px|thumb|Files involved when Swig is used for binding C++ to Python.<ref>Adapted from Ammar Nejati, Mikhail Svechnikov, Joachim Wuttke: Deploying a C++ Software with (or without) Python Embedding and Extension. In Bernoth et al, eds: deRSE24 - Selected Contributions of the 4th Conference for Research Software Engineering in Germany. [https://eceasst.org/index.php/eceasst/article/view/2596 ECEASST 83 (2025)].</ref>]] 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 [[Marshalling (computer science)|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 two forms: *a [[library (computer science)#Shared libraries|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:<ref>{{Cite web|url=https://www.swig.org/Doc4.2/SWIG.html#SWIG_nn9|title=SWIG Basics}}</ref> <syntaxhighlight lang="swig"> %module example %inline %{ extern double sin(double x); extern int strcmp(const char *, const char *); extern int Foo; %} #define STATUS 50 #define VERSION "1.1" </syntaxhighlight> In this file, there are two functions {{code|sin()}} and {{code|strcmp()}}, a global variable {{code|Foo}}, and two constants {{code|STATUS}} and {{code|VERSION}}. When SWIG creates an extension module, these declarations are accessible as scripting language functions, variables, and constants respectively. In Python: <syntaxhighlight lang="pycon"> >>> example.sin(3) 0.141120008 >>> example.strcmp("Dave", "Mike") -1 >>> print(example.cvar.Foo) 42 >>> print(example.STATUS) 50 >>> print(example.VERSION) 1.1 </syntaxhighlight> ==Purpose== There are two main reasons to embed a [[scripting language|scripting engine]] in an existing C/C++ program: *The program can then be customized far faster, [[Ousterhout's dichotomy|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 (computer science)|library]] which has no equivalent in the scripting language. *Write the whole program in the scripting language first, and after [[profiling (computer programming)|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 [[Component Object Model|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 (software)|SIP]], Pybind11, and [[Boost (C++ libraries)|Boost's Boost.python library]]. ==Projects Using SWIG== *[[ZXID]] (Apache License, Version 2.0) *Symlabs SFIS (commercial) *[[LLDB (debugger)|LLDB]] *[[GNU Radio]] up to (including) version 3.8.x.x; later versions use Pybind11<ref>{{cite web |last1=Müller |first1=Marcus |title=Changelog for GNU Radio 3.9.0.0 |website=[[GitHub]] |url=https://github.com/gnuradio/gnuradio/blob/v3.9.0.0/CHANGELOG.md}}</ref> *[[Xapian]] *[[TensorFlow]] *[[Apache SINGA]] *[[QuantLib]] *[[Babeltrace]] ==See also== {{Portal|Free and open-source software}} *[[Language binding]] *[[Foreign function interface]] (FFI) *[[Calling convention]] *[[Name mangling]] *[[Application programming interface]] *[[Application binary interface]] *[[Comparison of application virtual machines]] *[[Wrapper function]] *[[GIWS (software)|GIWS]] for the opposite of SWIG: calling Java from C/C++ ==References== {{Reflist}} ==Further reading== *Article "[https://www.codeguru.com/dotnet/expose-your-c-c-programs-internal-api-with-a-quick-swig/ Expose Your C/C++ Program's Internal API with a Quick SWIG]" by Victor Volkman *Article "[https://web.archive.org/web/20090129071001/http://geocities.com/foetsch/python/extending_python.htm Python Extensions In C++ Using SWIG]" by Michael Fötsch *Presentation "[https://web.archive.org/web/20090425013344/http://blip.tv/file/1179673/ Application overview for openSUSE]" by Klaus Kämpf ==External links== *{{Official website}} [[Category:Cross-platform software]] [[Category:Free computer programming tools]] [[Category:Programming tools]] [[Category:Scripting languages]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Infobox
(
edit
)
Template:Infobox software
(
edit
)
Template:Main other
(
edit
)
Template:Multiple issues
(
edit
)
Template:Official website
(
edit
)
Template:Other uses
(
edit
)
Template:Portal
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Template other
(
edit
)