Replies: 5 comments 19 replies
-
@JeWe37 That's an interesting approach. The restriction to a musl based system seems somewhat strict. Can such a build also be done on a glibc-based OS? By the way, this seems like more of a discussion than in issue. Will convert. |
Beta Was this translation helpful? Give feedback.
-
I agree that static linking with glibc is a bad idea. The libc library matches the kernel, so these are installed together on a system. The GNU C Library, For example, using
So, my extension uses Note that If you're willing to give up recording symbol versions at link time, I wonder if you can simply not link with libc at all. The python3 binary is linked with libc. So, maybe your extension will find the symbols it needs from whatever is available in the libc that the python3 executable itself has loaded. It's still dangerous--you don't have versioned symbols in myextension--but maybe that's a risk your willing to take if you only use basic, very stable libc functions. I do not know if this works or if there are other problems; I'm just brainstorming ideas..... |
Beta Was this translation helpful? Give feedback.
-
One point may be relevant to mention: the combination of nanobind and cibuildwheel already supports wheel builds where a Though I guess the main point is actually that the version of GCC or Clang might be newer, rather than static linking actually being the beneficial aspect. |
Beta Was this translation helpful? Give feedback.
-
But, aren't we talking about building a nanobind extension that will be dynamically loaded using |
Beta Was this translation helpful? Give feedback.
-
C++ One Definition Rule https://en.cppreference.com/w/cpp/language/definition
Informally,
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Please don't use.
As (linux) python modules are shared objects, they typically link to libc. For compatibility, they thus need to be built on a system which uses a libc which is older than the user system, as (g)libc is only forwards compatible. Typically this is addressed by standardizing on one of the manylinux variants as outlined in PEP 513, PEP 600 etc., however this requires that the project can be practically built on a system that old. This can often be challenging.
To address this, libc could be linked statically into the shared library, allowing the resulting shared object to be loaded on any linux system, even if built on a modern distribution, vastly reducing build complexity.
However, statically linking glibc is widely regarded as extremely poorly supported, and in fact distros such as Debian have not even shipped the required PIC variant of libc for years, that would be required for this. Yet, as statically linking libc means that which libc is used is irrelevant, so instead building a project on a modern musl libc based distro(e.g., alpine), will still allow it to be loaded even on glibc based distros.
Long story short, on any musl based system, the following CMake will generate a Python module that can be loaded regardless of distribution:
This will break if any other shared objects are linked into the module, but that can usually be avoided comparatively easily(and is preferable for redistribution anyway), so long as the complexity of libc is taken care of.
I am wondering if there would be interest in integrating this feature into nanobind. The above code can be used to allow the user to configure this, but in so far as I can tell nobody has ever proposed this approach to dealing with libc compatibility for python modules, meaning figuring this out was complex to say the least, but is currently working for my module.
I suspect this would make nanobind the only binder(for any language) that has this feature, at least I have never seen anyone mention it.
Beta Was this translation helpful? Give feedback.
All reactions