0

I have a C++ program that needs to make certain calls to a library which is written in Python. (As opposed to rewriting the entire library in C++...)

What's the simplest way to do this? I need to pass 2 numbers into the library, and get 2 numbers back.

I need it to be portable and run in both Windows and Linux, so for example using POSIX popen is not possible.

So far the best solution I have is to write a python wrapper, called by a "system" call in the C++ code, that takes command line parameters, calls the function, and writes the result to a file, which is then opened by the C++ program.

If you know anything better please let me know...

CaptainCodeman
  • 1,951
  • 2
  • 20
  • 33

2 Answers2

6

If boost is an option, you can use boost.python : embedding

Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 1
    The most relevant part of that doc is here: https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/tutorial/tutorial/embedding.html – Chronial Sep 16 '19 at 17:49
1

You can directly embed the python interpreter into your application. See the official documentation for a full explanation.

Chronial
  • 66,706
  • 14
  • 93
  • 99