0

I have legacy libraries written in C++ which is maintained by other team in other country. Currently we cannot alter the code of the libraries and are provided with just APIs to call from Python. I see boost and pybind11 as solutions but they all use #include<pybind11.h> in the C++ libraries. I can't do that.

Is there any other way?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
hrajora
  • 11
  • 1
  • 2
    Write a wrapper in C++ which you can call from Python. Then use the wrapper to call the legacy libraries. – Richard Critten Dec 22 '21 at 11:52
  • 1
    Create a wrapper library that can use pybind. – Pepijn Kramer Dec 22 '21 at 11:52
  • @RichardCritten Can you please give me any references for it? Its really urgent. – hrajora Dec 22 '21 at 11:53
  • 1
    Well seem you have found out about pybind. Start a new C++ project, and for all calls you need from python add one function to the new project, include pybind, and forward the calls to the existing libraries. It is not rocket science but may be a bit of work – Pepijn Kramer Dec 22 '21 at 11:54
  • Whatever you use, you have to write yet another library in C or C++ which provides the Python wrapper for your legacy C++ libraries. The most simple which comes into my mind, is [ctypes](https://docs.python.org/3/library/ctypes.html) but it might be limited concerning C++. – Scheff's Cat Dec 22 '21 at 11:54
  • 1
    FYI: [SO: Modern/2020 way to call C++ code from Python](https://stackoverflow.com/q/64084033/7478597) (and more with [google "site:stackoverflow.com Call c++ from Python"](https://www.google.com/search?q=site%3Astackoverflow.com+Call+c%2B%2B+from+Python)) – Scheff's Cat Dec 22 '21 at 11:56
  • Or : https://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html – Pepijn Kramer Dec 22 '21 at 11:56
  • Look at https://pybind11.readthedocs.io/en/stable/basics.html., Find the sentence "For simplicity 1, we’ll put both this function and the binding code..." Read through on what should be done in practice. The "implementation" is the other team's library, and "binding code " is `PYBIND11_MODULE` that you write. You put it not just in a separate file but in a separate project. – n. m. could be an AI Dec 22 '21 at 13:07
  • If you are short on time and need to expose a large C++ API, you may consider trying to autogenerate the bindings using something like https://github.com/kljohann/genpybind – n. m. could be an AI Dec 22 '21 at 13:13

0 Answers0