Skip to content

windows: guard static_assert with __STDC_VERSION__ for C99 compatibility#810

Merged
Youw merged 1 commit into
masterfrom
fix-c99-static-assert
May 20, 2026
Merged

windows: guard static_assert with __STDC_VERSION__ for C99 compatibility#810
Youw merged 1 commit into
masterfrom
fix-c99-static-assert

Conversation

@Youw
Copy link
Copy Markdown
Member

@Youw Youw commented May 20, 2026

Problem

windows/hidapi_descriptor_reconstruct.h:127 uses static_assert to sanity-check the in-memory layout of hid_pp_link_collection_node_:

static_assert(sizeof(struct hid_pp_link_collection_node_) == 16, ...);

static_assert was added to <assert.h> in C11. Building with -std=c99 — for example via set(CMAKE_C_STANDARD 99) in a parent CMake project, as reported in #764 — fails at that line on mingw/gcc:

windows/hidapi_descriptor_reconstruct.h:127:15: error: expected declaration specifiers or '...' before 'sizeof'

Change

The assertion's preceding comment describes it as a "risk-reduction-measure" rather than a strict requirement. Guard it:

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
static_assert(...);
#endif
  • C11 / C17 / C23 toolchains still get the compile-time check (behaviour unchanged).
  • C99 and earlier skip it and build cleanly.
  • MSVC in its default C mode (where __STDC_VERSION__ is typically not set that high) also skips it — a minor reduction in the safety net there, but the production build is unaffected.

Net diff: +4 / -0 in one file.

Verification

Reproduced and fixed locally with x86_64-w64-mingw32-gcc (GCC 10, MinGW-w64) in WSL, compiling the real backend sources:

build -std=c99 -std=c11
master (no fix) failerror: expected declaration specifiers or '...' before 'sizeof' at L127 pass
this PR pass pass

Tested with both windows/hid.c and windows/hidapi_descriptor_reconstruct.c (the two sources that include the affected header).

Closes #764.

Drafted with Claude Code.

windows/hidapi_descriptor_reconstruct.h uses static_assert() to
sanity-check the layout of hid_pp_link_collection_node_. static_assert
was added to <assert.h> in C11; building with -std=c99 (e.g.
set(CMAKE_C_STANDARD 99) in a parent project) on mingw/gcc fails at
that line because the identifier is undeclared.

The assertion's preceding comment describes it as a "risk-reduction
measure" rather than a strict requirement, so guard it with
`#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L`.
C11/C17/C23 toolchains still get the compile-time check; older
standards (and MSVC default C mode, where __STDC_VERSION__ is not
typically set that high) skip it and build cleanly.

Closes: #764

Assisted-by: Claude:claude-opus-4.7
@Youw Youw marked this pull request as draft May 20, 2026 21:09
@Youw Youw marked this pull request as ready for review May 20, 2026 21:09
@Youw Youw merged commit 5bec589 into master May 20, 2026
23 checks passed
@Youw Youw deleted the fix-c99-static-assert branch May 20, 2026 21:09
@mcuee mcuee added the Windows Related to Windows backend label May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Windows Related to Windows backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C99 compile - static_assert failure

2 participants