ITADN

[BUG]: Cannot compile emscripten/pyodide project

#6043Openmducle 创建于 2026-04-26
triage
M
mduclecommented
### Required prerequisites - [x] Make sure you've read the [documentation](https://pybind11.readthedocs.io). Your issue may be addressed there. - [x] Search the [issue tracker](https://github.com/pybind/pybind11/issues) and [Discussions](https:/pybind/pybind11/discussions) to verify that this hasn't already been reported. +1 or comment there if it has. - [ ] Consider asking first in the [Gitter chat room](https://gitter.im/pybind/Lobby) or in a [Discussion](https:/pybind/pybind11/discussions/new). ### What version (or hash if on master) of pybind11 are you using? 3.0.4 ### Problem description I'm working on a project using pybind11 and CMake for pyodide/webassembly. With newer versions of CMake (`>=4.1.0`) and pybind11 (`>=3.0.2`) the cross-compilation gives an error. Using either older CMake (`<4.1.0`) and newer pybind (`>=3.0.2`) **or** new CMake and old pybind gives successful compilation. I think this is caused by PR #5829 which only sets the internal variable `_PYBIND11_CROSSCOMPILING` if the `CMAKE_CROSSCOMPILING_EMULATOR` is *not* defined ([code](https://github.com/pybind/pybind11/pull/5829/changes#diff-b1088d71d1d5f7fe8b6f2c6f8a8b4e3437c4e653344c445d0bf077615c2f8457R53-R55)) - however, [policy CMP0190](https://cmake.org/cmake/help/git-stage/policy/CMP0190.html) introduced by CMake `4.1` actually *requires* this to be defined and set to the program used to execute the cross-compiler. As such when running CMake with the emscripten cross-compiler, `CMAKE_CROSSCOMPILING_EMULATOR` is always defined which means the check fails and `_PYBIND11_CROSSCOMPILING` is always set to `OFF` and the compile then fails when the pointer size does not match (webassembly is 32-bit, the host system is 64-bit. This can be reproduced with the [cmake_example](https://github.com/pybind/cmake_example) project with some [modifications](https://github.com/mducle/cmake_example/commit/149a72dde62f9a90fbf7de203a52bbe28c1cfb60) in a fork: ``` git clone --recurse-submodule https://github.com/mducle/cmake_example cd cmake_example/pybind11 git checkout v3.0.4 cd .. python -m pip install cibuildwheel cmake CIBW_BUILD="cp312*" python -m cibuildwheel --platform pyodide ``` This will give an error `Cannot run the interpreter` but this is spurious - it's because the cross-compiling flag is not set correctly. Now, I can get the compilation to work by: * Changing cmake version to `4.0.0` or earlier in the pip install * Changing pybind version to `3.0.1` or earlier in the checkout * Keep both versions up-to-date and editing `tools/pybind11Common.cmake` to remove the `NOT` in line 55 ### Reproducible example code ```text ``` ### Is this a regression? Put the last known working version here if it is. 3.0.1
5 条评论