fix(cmake): don't link SHARED modules against libpython in FindPython mode - #6140
Open
henryiii wants to merge 1 commit into
Open
fix(cmake): don't link SHARED modules against libpython in FindPython mode#6140henryiii wants to merge 1 commit into
henryiii wants to merge 1 commit into
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
henryiii
marked this pull request as ready for review
August 7, 2026 12:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:SHAREDnow calls plainadd_libraryinstead ofPython_add_library, andpybind11::moduleinstead ofpybind11::embed(onlySTATICkeepspybind11::embed).This matches what classic mode (
pybind11Tools.cmake) has always done forSHARED, so it aligns the two code paths rather than inventing new semantics. Verified locally on macOS arm64 with a uv-managed CPython 3.13: the built module no longer linkslibpython, the previously-failinginstalled_functioncase passes, and all othertest_cmake_buildcases (including both embed cases) still pass.Behavior notes:
SHAREDno longer requires theDevelopment.Embedcomponent and no longer records alibpythondependency. An application that links aSHAREDpybind11 library into an embedding executable must linklibpythonitself — already the case in classic mode.SHARED+WITH_SOABIpreviously produced an author warning from FindPython ("MODULE only") and was a no-op; it now fails at configure becauseWITH_SOABIis no longer parsed out. Left unhandled to keep the change minimal.Suggested changelog entry:
pybind11_add_modulein FindPython mode no longer linksSHAREDextension modules againstlibpython, matching classic mode; this fixes import crashes with statically linked interpreters (e.g. uv / python-build-standalone) on macOS.