Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

SWIG

From Wikipedia, the free encyclopedia
Open-source programming tool
For other uses, seeSwig.
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(August 2017) (Learn how and when to remove this message)
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "SWIG" – news ·newspapers ·books ·scholar ·JSTOR
(August 2017) (Learn how and when to remove this message)
(Learn how and when to remove this message)
SWIG
Original authorDavid M. Beazley
DeveloperSWIG developers
Initial releaseFebruary 1996; 30 years ago (1996-02)[1]
Stable release
4.3.0[2] Edit this on Wikidata / 20 October 2024; 15 months ago (20 October 2024)
Written inC,C++
LicenseGPLv3
Websiteswig.org
Repository

TheSimplified Wrapper and Interface Generator (SWIG) is anopen-source software tool used to connectcomputer programs orlibraries written inC orC++ withscripting languages such asLua,Perl,PHP,Python,R,Ruby,Tcl, and other language implementations likeC#,Java,JavaScript,Go,D,OCaml,Octave,Scilab andScheme. Output can also be in the form ofXML.

Function

[edit]
Files involved when Swig is used for binding C++ to Python.[3]

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 generateconversion code for functions with simple arguments; conversion code for complex types of arguments must be written by the programmer. The SWIG tool createssource code that provides the glue between C/C++ and the target language. Depending on the language, this glue comes in three forms:

  • ashared 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, usingJava Native Interface (JNI) in Java).
  • a shareddynamic 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

[edit]

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:[4]

%moduleexample%inline%{externdoublesin(doublex);externintstrcmp(constchar*,constchar*);externintFoo;%}#define STATUS 50#define VERSION "1.1"

In this file, there are two functionssin() andstrcmp(), a global variableFoo, and two constantsSTATUS andVERSION. 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

[edit]

There are two main reasons to embed ascripting 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 afterprofiling, rewrite performance-critical code in C or C++.

History

[edit]

SWIG is written in C and C++ and has been publicly available since February 1996. The initial author and main developer wasDavid M. Beazley who developed SWIG while working as a graduate student atLos Alamos National Laboratory and theUniversity of Utah and while on the faculty at theUniversity of Chicago. Development is currently supported by an active group of volunteers led by William Fulton. SWIG has been released under aGNU General Public License.

Google Summer of Code

[edit]

SWIG was a successful participant ofGoogle 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 generatingCOM 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 aScilab module. Matevz Jekovec spent time onC++0x features. Ashish Sharma spent his summer on anObjective-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 andDoxygen comments.

Alternatives

[edit]

For Python, similar functionality is offered bySIP, Pybind11, andBoost's Boost.python library.

Projects using SWIG

[edit]

See also

[edit]

References

[edit]
  1. ^"SWIG History".
  2. ^"Release 4.3.0". Retrieved10 December 2024.
  3. ^Adapted from Ammar Nejati, Mikhail Svechnikov, Joachim Wuttke: Deploying a C++ Software with (orwithout) Python Embedding and Extension. In Bernoth et al, eds: deRSE24 - Selected Contributions of the 4th Conference forResearch Software Engineering in Germany.ECEASST 83 (2025).
  4. ^"SWIG Basics".
  5. ^Müller, Marcus."Changelog for GNU Radio 3.9.0.0".GitHub.

Further reading

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=SWIG&oldid=1333190290"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp