How to Create a Python Wrapper for C/C++ Shared Libraries

Utilizing the ctypes module to call C/C++ functions in Python

Ng Wai Foong
Better Programming
Published in
6 min readApr 13, 2020

--

Photo by Chris Ried on Unsplash

By reading this article, you will learn to create and implement a wrapper class to call C/C++ functions directly in Python. We will use a built-in module called ctypes. According to the official documentation, ctypes is

“a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.”

There are three sections in this tutorial:

  1. Setup
  2. Implementation
  3. Conclusion

Let’s proceed to the next section to set up the required materials for this tutorial.

1. Setup

I will be using Python 3.7.4 for this tutorial. Feel free to use other versions of Python as long as you can call the built-in module ctypes. It is highly recommended to create a virtual environment for this project.

Make sure that you have a dll that is ready to be used by our Python file. You only need the dll file for creating the wrapper. A header file is optional if you have access and know the available functions and parameters. If you…

--

--