SWIG is a software development tool that facilitates connecting programs written in C and C++ with a variety of high-level programming languages, including Python. It works by generating 'wrapper' code that allows scripting languages to access underlying C/C++ code. The current version is 4.4.1, with releases focused on C/C++ standard improvements, expanded language support, and Python compatibility updates.
pip install swigVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the full SWIG workflow: defining C/C++ functions, creating a SWIG interface file (`.i`), using the `swig` command to generate Python wrapper code and a Python module file, compiling the C/C++ source with the generated wrapper, linking them into a shared library, and finally importing and using the module in Python. Note that the compilation commands (g++) are platform-dependent and assume a C++ compiler and Python development headers are installed.
Review your build system and generated wrapper code for compatibility with heap types and multi-phase initialization. If using `Py_LIMITED_API`, ensure compatibility, as fixes for combinations with recent Python versions were added in 4.4.1.
Avoid `from <module> import *` when dealing with multiple SWIG modules. Instead, use `import <module_name>` and access global variables via `<module_name>.cvar.<variable_name>`.
Ensure your compilation/linking step creates a shared library named `_<module_name>.so` (or `_<module_name>.pyd` on Windows) that matches the `%module` directive in your `.i` file.
Provide explicit definitions for opaque types (e.g., `typedef void* HWND;` in the interface file) or use custom typemaps to instruct SWIG on how to manage the memory for such pointers, for instance, by treating handles as integers.
Carefully ensure that C++ namespaces are correctly set in your SWIG interface file, especially when dealing with classes and callbacks that pass parameters by reference. This helps SWIG correctly map the C++ types to their Python representations.
No dependency data recorded yet.