Repository navigation
Conversation
… mode Python_add_library() hard-links non-MODULE libraries against Python::Python. On macOS with a statically linked interpreter (e.g. uv / python-build-standalone), importing such a module loads a second, uninitialized copy of the Python runtime and aborts with a fatal PyInterpreterState_Get error. Bypass python_add_library for SHARED and link pybind11::module instead of pybind11::embed, matching the classic pybind11Tools.cmake behavior. Assisted-by: ClaudeCode:claude-fable-5
Assisted-by: ClaudeCode:claude-opus-5-5
c538993 to
41db6b8
Compare
|
— codex on behalf of Ralf — Preserve or document the executable-linking contract for Generated by Codex. Ralf has yet to review these findings. |
…forms An executable that links only a SHARED pybind11 library failed to link on Linux, because the library no longer recorded its libpython dependency. Limit the change to Apple platforms, where the libpython link breaks import from a statically linked interpreter, and to the case where Development.Embed is not available. Document when an executable must link pybind11::embed itself. Assisted-by: ClaudeCode:claude-fable-5-2
|
🤖 AI text below 🤖 The point is valid. Fixed in 48f8ca5.
The PR description and changelog entry are updated. |
|
🤖 AI text below 🤖 Linux results for 48f8ca5. Image: official
|
Our own test suite fails if using python-build-standalone (or any other statically linked Python). This is one option for a fix.
🤖 AI text below 🤖
Description
In FindPython mode,
pybind11_add_module(... SHARED ...)routes through CMake'sPython_add_library, which hard-links every non-MODULElibrary againstPython::Python. An extension module must not linklibpythonon macOS: with a statically linked interpreter (uv / python-build-standalone), the linkedlibpython3.x.dylibloads as a second, uninitialized copy of the Python runtime, two-level namespace binds the module's C-API calls to it, and the import aborts with:This is exactly what the
installed_functioncase oftest_cmake_build(the only case that usesSHARED) hits when the repo's documentedcmake --workflow venvflow (uv-created venv) runs on macOS. Linux hides the same duplicate-runtime setup through ELF symbol interposition, and macOS CI uses dynamically linked python.org builds, so CI never sees it.Fix, in
pybind11NewTools.cmake.SHAREDskipsPython_add_libraryand linkspybind11::moduleinstead ofpybind11::embedin two cases:Development.Embed(this configuration was an error before).On other platforms with
Development.Embed,SHAREDis unchanged and still linkslibpython, so an executable can link the library alone.STATICis unchanged.Verified locally on macOS arm64 with a uv-managed CPython: the built module no longer links
libpython, the previously-failinginstalled_functioncase passes, and all othertest_cmake_buildcases still pass.Behavior notes:
SHAREDlibrary no longer records alibpythondependency. An executable that links it must also linkpybind11::embed(already the case in classic mode). This is now in the docs, andsubdirectory_functiontests it.SHARED+WITH_SOABIis accepted and has no effect, as before.Suggested changelog entry:
pybind11_add_modulein FindPython mode no longer linksSHAREDextension modules againstlibpythonon Apple platforms; this fixes import crashes with statically linked interpreters (e.g. uv / python-build-standalone). An executable that links such a library must linkpybind11::embed.SHAREDalso no longer requiresDevelopment.Embed.