SWIG: Calling C++ code from Python

Byte Blog
3 min readApr 28, 2023

--

Building a Python Module using C++ and SWIG

If you're a C++ developer looking to integrate your code into a Python application, you may be wondering how to build a Python module using C++. One option is to use SWIG (Simplified Wrapper and Interface Generator), a tool that makes it easy to create interfaces between C++ code and other languages.

In this tutorial, we'll walk through the steps required to create a Python module using C++ and SWIG.

Step 1: Install SWIG

First, you'll need to install SWIG. SWIG is available for most operating systems and can be downloaded from the official SWIG website. Once you've downloaded SWIG, you can install it by following the instructions for your operating system.

Step 2: Write Your C++ Code

Next, you'll need to write your C++ code. For this example, we'll create a simple C++ class that adds two numbers together:

// adder.h
class Adder {
public:
Adder(int a, int b);
int add();
private:
int a_;
int b_;
};

// adder.cpp
Adder::Adder(int a, int b) : a_(a), b_(b) {}
int Adder::add() {
return a_ + b_;
}

Step 3: Write the SWIG Interface File

The next step is to write the SWIG interface file. This file tells SWIG which C++ classes and functions to generate Python bindings for. Here's what the SWIG interface file for our Adder class looks like:

// adder.i
%module adder
%{
#include "adder.h"
%}

%include "adder.h"

This interface file tells SWIG to generate a Python module called "adder", and to include the "adder.h" header file. We also need to tell SWIG to generate bindings for our Adder class by including the "adder.h" header file.

Step 4: Generate the Wrapper Code

Now that we have our C++ code and SWIG interface file, we can use SWIG to generate the wrapper code. To do this, we run the following command:

swig -c++ -python adder.i

This generates a set of C++ wrapper code that provides a Python interface to our Adder class.

Step 5: Build the Python Module

Finally, we can build the Python module. To do this, we need to compile the generated C++ wrapper code into a shared library that can be loaded by Python. Here's how to build the module on Linux:

g++ -fPIC -c adder_wrap.cxx -I/usr/include/python3.8
g++ -shared adder_wrap.o -o _adder.so

On Windows, you can use the following commands to build the module:

cl.exe /MD /LD /EHsc adder_wrap.cxx /I C:\Python38\include /Fe_adder.pyd

These commands compile the generated C++ wrapper code into a shared library named "_adder.so" (on Linux) or "_adder.pyd" (on Windows).

Step 6: Test the Python Module We're now ready to test our Python module. Here's an example Python script that uses our Adder module:

import adder

a = adder.Adder(1, 2)
print(a.add()) # Output: 3

Running this script should produce the output "3", indicating that our Python module is working correctly.

Conclusion

In this tutorial, we walked through the steps required to create a Python module using C++ and SWIG. We started by installing SWIG and writing a simple C++ class. We then wrote the SWIG interface file and used SWIG to generate the wrapper code. Finally, we built the Python module and tested it using a simple Python script.

While this tutorial provided a basic example, SWIG can be used to generate Python bindings for much more complex C++ code. If you’re interested in learning more, we recommend checking out the official SWIG documentation.

Thank you for reading, and we hope this tutorial was helpful in getting you started with building Python modules using C++ and SWIG!

--

--

Byte Blog
Byte Blog

Written by Byte Blog

Technology enthusiast with a passion for transforming complex concepts into bite sized chunks

No responses yet