0

I have several (let's say 5-10) large C++ projects. Think around 30-50 files each. These projects are maintained by other teams and I cannot modify their code. At all.

I need to call these projects inside a Python script that is supposed to call functions from all of the projects (obviously each of the top-level functions from these projects creates a large call tree spanning multiple files).

Is there a way to call C++ functions in Python from these large C++ projects without modifying any C++ code?

Rares Dima
  • 1,575
  • 1
  • 15
  • 38

1 Answers1

0

Yes, it is possible. Major libraries like OpenCV are implemented in C++ and have Python bindings to use them from Python.

There are many competing ways of achieving this, and you can read an overview here. I believe the most common ones nowadays are Cython and PyBind11.

Calling C or C++ from Python basically involves defining an interface that Python can understand (translating to and from the data types that C and C++ code uses), and compiling that interface. Tools like Cython allow you to write that interface in a language close to Python, and generate the corresponding, rather ugly, C++ interface for you so it can be compiled.

Note that you will only have to write interfaces for the functions you want to use, regardless of the "call tree" that these functions will generate. You won't have to touch the C++ projects at all, only add interfaces that can live in your project.

francoisr
  • 4,407
  • 1
  • 28
  • 48