mypyc-compiled extensions missing math library linking causing undefined symbol errors
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
When building mypy with mypyc enabled (MYPY_USE_MYPYC=1), the compiled C extensions are missing explicit linking to the math library (libm), causing undefined symbol errors for standard math functions.
[./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so]:
linux-vdso.so.1 (0x0000ffffb4c9c000)
libc.so.6 => /lib64/libc.so.6 (0x0000ffffb4632000)
/lib/ld-linux-aarch64.so.1 (0x0000ffffb4c4f000)
undefined symbol: sin (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: cos (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: tan (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: sqrt (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: exp (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: fmod (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: pow (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: log (./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
[./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so]:
linux-vdso.so.1 (0x0000ffff80b96000)
libc.so.6 => /lib64/libc.so.6 (0x0000ffff7ee32000)
/lib/ld-linux-aarch64.so.1 (0x0000ffff80b49000)
undefined symbol: sin (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: cos (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: tan (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: sqrt (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: exp (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: fmod (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: pow (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: log (./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
To Reproduce
PACKAGE_VERSION="v1.19.1"
ARCHIVE_NAME="${PACKAGE_VERSION}.tar.gz"
curl -fLO "https://github.com/python/mypy/archive/refs/tags/${ARCHIVE_NAME}"
tar xf "${ARCHIVE_NAME}"
cd "mypy-${PACKAGE_VERSION#v}"
export MYPY_USE_MYPYC="1"
pip3 install . -v -v -v --compile --no-deps --no-cache-dir --no-build-isolation --no-binary :all:
Expected Behavior
No unexpected undefined symbols
Actual Behavior
Unexpected undefined symbols
Your Environment
- OS: Amz Linux 2023 aarch64
- Python: 3.12.7
- mypy: 1.19.1
Proposed Solution
The math library needs to be added to the extension modules during the build process. A potential patch location is in setup.py after the extensions are created:
# After mypyc_build_ext() creates ext_modules, add math library
for ext in ext_modules:
if "m" not in ext.libraries:
ext.libraries.append("m")
However, this approach may over-link modules that don't need libm. A more precise solution would require:
- Identifying which specific mypyc-generated extensions actually use math functions
- Only adding
-lmto those specific extensions - This may require changes in mypyc itself to track which C math functions are used during compilation
Temporary Workaround is:
# Patch setup.py to add math library to all extensions
sed -i '
/log_trace=log_trace,$/,/^ assert ext_modules$/ {
/^ )$/ a\
\
for ext in ext_modules:\
if "m" not in ext.libraries:\
ext.libraries.append("m")
}
' "./setup.py"
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in setup.py bei mypyc_build_ext() und der Erstellung von ext_modules. Führe anschließend den Build mit MYPY_USE_MYPYC=1 unter Verwendung des dokumentierten pip-Befehls in der betroffenen Umgebung erneut durch. Ermittle, welche generierten Erweiterungen libm benötigen, und stelle sicher, dass die erstellten Erweiterungen ohne undefinierte Mathe-Symbole geladen werden, wobei unnötiges Linking nach Möglichkeit vermieden wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- build-system
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100