请输入您要查询的百科知识:

 

词条 SWIG
释义

  1. Function

  2. Example

  3. Purpose

  4. History

  5. Google Summer of Code

  6. Alternatives

  7. Projects Using SWIG

  8. See also

  9. References

  10. Further reading

  11. External links

{{Other uses|Swig (disambiguation){{!}}Swig}}{{more footnotes|date=August 2017}}{{refimprove|date=August 2017}}{{Infobox software
| name = SWIG
| author = David M. Beazley
| developer = SWIG developers
| released = {{Start date and age|1996|02}}[1]
| latest release version = 3.0.12
| latest release date = {{Start date and age|2017|01|27}}
| programming language = C, C++
| license = GPL
| website = {{URL|swig.org}}
}}

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 languages like C#, Java, JavaScript, Go, Modula-3, OCaml, Octave, Scilab and Scheme. Output can also be in the form of XML or Lisp S-expressions.

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

%module example

%inline %{

extern double sin(double x);

extern int strcmp(const char *, const char *);

extern int Foo;

%}

  1. define STATUS 50
  2. define VERSION "1.1"

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:

>>> 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 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 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
  • QuantLib
  • Babeltrace

See also

{{Portal|Free and open-source software}}
  • Language binding
  • Foreign function interface
  • 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

1. ^http://www.swig.org/history.html
2. ^http://www.swig.org/Doc3.0/SWIG.html#SWIG_nn9

Further reading

  • Article "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}}
{{DEFAULTSORT:Swig}}

4 : Computer programming tools|Cross-platform software|Free computer programming tools|Scripting languages

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/13 22:47:13