Generated by GPT-5-mini| SWIG | |
|---|---|
| Name | SWIG |
| Developer | David Beazley |
| Released | 1996 |
| Programming language | C (programming language), C++ |
| Operating system | Unix, Microsoft Windows, macOS |
| License | LGPL |
SWIG is a software development tool for generating bindings between compiled languages and high-level scripting languages. It automates creation of wrapper code allowing programs and libraries written in C (programming language), C++ and other native languages to be invoked from environments such as Python (programming language), Perl, Ruby, Java, and R. By reducing manual glue code, it facilitates reuse of existing libraries across projects involving tools and ecosystems like NumPy, TensorFlow, Qt, Boost, and OpenCV.
SWIG functions as an interface generator, translating C and C++ declarations into wrapper modules that can be loaded into host interpreters and runtimes such as CPython, JRuby, GNUstep, JVM, and Mono environments. It supports typemaps, directors, and multiple output languages to adapt calling conventions for platforms including Linux, FreeBSD, Microsoft Windows, and macOS. Its design targets interoperability scenarios found in projects relying on libraries like SQLite, OpenSSL, FFmpeg, GTK, and LLVM.
Development began in the mid-1990s by David Beazley to address challenges in integrating C/C++ code with languages such as Tcl, Perl, and Python. Early adoption grew through use in scientific and engineering projects associated with institutions like Los Alamos National Laboratory and collaborations involving tools like SciPy, Matplotlib, and Octave. Over successive releases SWIG added support for languages such as Java, C#, Lua, and R, influenced by interoperability work in communities around Apache Software Foundation projects and standards like POSIX. Contributors have included maintainers and developers affiliated with organizations such as Google, IBM, Microsoft, and academic labs that leverage native libraries in bindings for research platforms.
SWIG uses a parser and code-generation backend that consumes C/C++ headers and interface files to produce wrapper source code tailored for a target language runtime like CPython, JVM, or GNUstep-based systems. Core components include the front-end lexer/parser, abstract syntax tree, code generators, and typemap system which maps native types to target-language representations; typemaps are crucial when interfacing libraries such as OpenSSL, libcurl, GTK, and Boost. The director feature provides callback and subclassing support for object-oriented interactions with frameworks like Qt and wxWidgets, enabling patterns used in projects like KDE or GNOME. Platform-specific code generation considers calling conventions and binary interfaces on x86, ARM, and PowerPC architectures, and integrates with build systems such as CMake, Autoconf, and Make.
SWIG supports numerous target languages, historically starting with Tcl, Perl, and Python and expanding to Java, C#, Lua, Ruby, R, Go, OCaml, Scala, and D. It generates code compatible with virtual machines and runtimes including the JVM, CLR, and various native interpreters used by projects like Blender and GIMP. Platform portability enables use in ecosystems across Linux distributions like Debian, Red Hat Enterprise Linux, and Ubuntu, and on Microsoft Windows systems used in conjunction with toolchains such as MinGW and Visual Studio.
Typical workflows involve writing an interface (.i) file that declares functions, classes, and enums from a native library—examples include small bindings for SQLite, wrappers for libxml2, or adapters for OpenCV—then invoking SWIG to produce wrapper source files which are compiled and linked into extension modules for target runtimes like CPython or the JVM. Common uses appear in scientific stacks relying on NumPy, machine learning frameworks interfacing with TensorFlow components, and GUI applications bridging Qt widgets to scripting languages for rapid prototyping. Example patterns include using typemaps for memory management when exposing malloc/free-based APIs, director classes for callback integration with GTK event loops, and custom code injections for integrating with build systems such as CMake or packaging tools like those used by Debian and Homebrew.
SWIG is developed by an open-source community with contributions from independent developers, academics, and engineers at companies including Google, IBM, and Microsoft. Discussion and collaboration occur on mailing lists, issue trackers, and code hosting platforms used by projects such as GitHub, with documentation and examples maintained to support integrations in ecosystems like SciPy, OpenCV, and TensorFlow. The project interacts with standards and projects such as POSIX, C++ Standards Committee, and third-party libraries like Boost and LLVM to expand language support and maintain compatibility across platforms and toolchains.
Category:Software