python / python/cpython

Python.h conflicts with stdlib.h if included after that

Offen
#155,321 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

build type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

Issue: Inconsistent _POSIX_C_SOURCE definition causes compilation errors when Python.h is included after system headers

Environment:

  • Fedora 44
  • CPython 3.14 (distro-provided packages)
  • Go 1.26 with CGo

Description:

When Python.h is included after system headers such as stdlib.h, a compilation error occurs due to conflicting definitions of the _POSIX_C_SOURCE macro. The conflict arises because:

  • Python.h indirectly defines _POSIX_C_SOURCE via its inclusion of pyconfig-64.h (located at /usr/include/python3.14/pyconfig-64.h)
  • System headers like stdlib.h also define _POSIX_C_SOURCE via their inclusion of /usr/include/features.h
  • The two definitions differ, triggering a compiler diagnostic

Context:

While the official CPython documentation recommends including Python.h before any other headers, this is not always feasible. In my specific use case, I'm using the libpython3.so shared library from Go via CGo. The CGo toolchain automatically generates a C helper file that puts #include <stdlib.h> before any user-provided code, making it impossible to adhere to the "include Python.h first" recommendation.

Current workaround:

I'm currently using #undef _POSIX_C_SOURCE immediately before including Python.h:

#undef _POSIX_C_SOURCE
#include <Python.h>

However, this approach is fragile and potentially unsafe, as it may cause system headers to be compiled with different _POSIX_C_SOURCE values across translation units, leading to subtle ABI mismatches or undefined behavior.

Proposed fix:

Modify the generated pyconfig-*.h files to guard the _POSIX_C_SOURCE definition with #ifndef/#endif preprocessor directives, similar to how other feature test macros are often handled:

#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L  /* or whatever version is currently used */
#endif

This change would make the definition idempotent and prevent compilation errors regardless of include order, while maintaining the intended feature test macro settings.

Impact:

This issue affects any project that:

  • Uses CPython's C API
  • Cannot control the include order of system headers (e.g., via CGo, SWIG, or other binding generators)
  • Builds on platforms where _POSIX_C_SOURCE is defined by system headers

Cc: @sobolevn

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-155343

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Überprüfung des im Issue verlinkten PRs gh-155343 und der generierten pyconfig-*.h-Dateien, die an der Definition von _POSIX_C_SOURCE beteiligt sind. Vergleichen Sie, wie sich Python.h verhält, wenn es nach stdlib.h eingebunden wird, und verwenden Sie anschließend das gemeldete Setup mit Fedora 44 und CPython 3.14, um den Kompilierungsfehler zu reproduzieren. Als erledigt gilt die Aufgabe, wenn der include-order-Fall keine widersprüchlichen Makrodiagnosen mehr erzeugt, ohne inkonsistente feature-test-Einstellungen einzuführen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, 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
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.