I want to create an extensible app via plugins with C++ and Python

355 Views Asked by At

I want to create an app that will be extensible via plugins.

I know that I have 2 options.

  1. I can create my own interpreted language and app with a built-in interpreter for this language.
  2. I can use one of the existing languages such as Python, Lua or another scripting language.

I want to use option 2. And I know that I must create a layer for external language to enable communication between this language and my app. But I don't know how to do it. Maybe I must use interprocess communication or something like that.

Let's assume that I have an application written in C++. In the beginning, it may be even a simple console app that displays a few options. And I want to write a plugin in Python like this:

option = "additional option"
myApp.addOption(option)

And then:

  1. I launch my app

  2. My app loads the plugin

  3. I see my app with this additional option displayed

I want to do this simple thing to understand how it works and then I will be able to do something more complicated.

2

There are 2 best solutions below

0
Basile Starynkevitch On BEST ANSWER

You should be aware that, with care, a C++ library can be called from a C program, mostly by appropriately using extern "C" to disable name mangling. On Linux, read also the C++ dlopen mini Howto.

Then you need to read the chapter Extending and embedding the Python interpreter

At last, Python is open source, so please study its source code.

I can use one of the existing languages such as Python, Lua or another scripting language.

I strongly recommend considering using GNU Guile or extending Ocaml.

And both TensorFlow or NumPy could inspire you, since they are open source libraries (coded in C and/or C++) usable from Python.

0
Andrew Mbugua On

You could start by looking at the languages' documentation(if you're new):

Python -->https://docs.python.org/3/ Lua --> https://www.lua.org/docs.html

C++ libraries can also be called in C(If you're careful enough),you could look at this too https://www.teddy.ch/c++_library_in_c/