Embedding Python in a C++ self-contained executable

You’ll need to rebuild Python as a static library and then reference that from your app. This should be easy enough on Linux, though on Windows you probably need a few patches (GitHub - indygreg/python-build-standalone: Produce redistributable builds of Python should have them, they just haven’t all been upstreamed yet).

Note that on Windows you’ll also need to rebuild any extension modules you want to support, either statically linked or directed to look into your executable for Python APIs rather than python311.dll. I don’t believe this is necessary on Linux, but I also don’t know exactly how to make it work.

You might have an easier time bundling the embeddable distro with your app on Windows, as this is the scenario it’s designed for. It’s stripped down and uses some features to avoid loading other copies of Python from the user’s machine, so it’s about as simple and secure as we can make it. You would probably omit the python.exe from that package in your case, as well as any extension modules you don’t want to support.

1 Like