Building and distributing C++ shared libraries with Python packages

I’ve drafted a tutorial on how to build and distribute a C++ shared library alongside a Python package. Here it is: poetry-install-shared-lib-demo.

I’m posting this here for anyone who is interested in following the steps PyStan is taking to adapt to the changes in Stan Math 3.0. (Whereas Stan Math 2.x could be used header-only, Stan Math 3.0 requires PyStan to use a shared library.)

I’m also posting this here in the hopes that someone can submit a pull request which makes the demo work under Windows and macOS. (It may already work under macOS, I haven’t checked.)

If we can get this demo working under Linux, macOS, and Windows, this will give us a fairly clear path towards a version of PyStan which works with the current release of Stan.


If this works, then fine. Another option could be to use swig, I think…as long as some gpl code is fine for you. I saw this being used in a project I recently came across.

I’m not sure if swig works for this. We are not wrapping anything to python but include external C++ lib.

The use of Cython is incidental. The focus is getting the shared library installed via Python build machinery (without too much fuss).

You raise a good point though. This demo could easily use some other kind of C++ wrapping tool. I used Cython since I’m familiar with it.