Skip to content

Make the Linux and Windows backends compile as C++#1848

Open
Youw wants to merge 3 commits into
masterfrom
cxx-build-fixes
Open

Make the Linux and Windows backends compile as C++#1848
Youw wants to merge 3 commits into
masterfrom
cxx-build-fixes

Conversation

@Youw
Copy link
Copy Markdown
Member

@Youw Youw commented Jun 6, 2026

Summary

Building libusb as C++ (useful for static-analysis tools and compiler flags that only run in C++ mode) needs a set of explicit casts plus a couple of C-vs-C++ keyword differences. #1782 makes the common code + the Darwin backend build as C++; this PR is the orthogonal companion covering the Linux and Windows pieces #1782 doesn't touch. It's independent of #1782 (no overlapping changes) and can land before or after it.

What it fixes

  • libusbi.h (atomics) — g++ can't include <stdatomic.h> in C++ mode before C++23 (the _Atomic keyword); this was by far the biggest blocker on Linux (>400 cascading errors). Use the GCC __atomic builtins for any GCC C++ build (previously only the Haiku path did). C and clang builds are unchanged.
  • os/threads_posix.c — map C11 _Thread_local to thread_local under __cplusplus (g++ rejects _Thread_local in C++).
  • os/linux_usbfs.c — explicit casts on void* allocations and usbi_get_*_priv() accessors; cast an int transfer status to its enum; reorder the usbi_os_backend initializer to the struct's declaration order (C++ requires designated initializers in order).
  • Windows backend (windows_common.{c,h}, windows_winusb.{c,h}, windows_usbdk.c, events_windows.c) — explicit casts on void* allocations/accessors; compare GUIDs with memcmp (IsEqualGUID takes references in C++); one enum→uint8_t narrowing cast for /WX.

All casts are no-ops in C and preserve behavior.

Verification (reproduced locally on Windows + WSL)

The C++-build CI proposed in #1845 exercises exactly this.

Refs: #1782
Assisted-by: claude-code:claude-opus-4-8

Building libusb as C++ (useful for analysis tools and compiler flags that
only run in C++ mode) needs explicit casts plus a few C/C++ keyword
differences. PR #1782 makes the common code and the Darwin backend build
as C++; this change is the orthogonal companion covering the Linux and
Windows pieces it does not touch.

- libusbi.h: use the GCC __atomic builtins instead of <stdatomic.h> when
  compiling as C++ with GCC. g++ cannot parse <stdatomic.h> in C++ mode
  before C++23 (the _Atomic keyword); previously only the Haiku path
  worked around this. C and clang builds are unchanged.
- os/threads_posix.c: map C11 _Thread_local to thread_local under C++.
- os/linux_usbfs.c: explicit casts on void*-returning allocations and
  usbi_get_*_priv() accessors, cast an int transfer status to its enum,
  and reorder the usbi_os_backend initializer to declaration order
  (C++ requires designated initializers in order).
- os/windows_common.{c,h}, os/windows_winusb.{c,h}, os/windows_usbdk.c,
  os/events_windows.c: explicit casts on void*-returning allocations and
  accessors; compare GUIDs with memcmp (IsEqualGUID takes references in
  C++); cast an enum to uint8_t to avoid a narrowing warning under /WX.

All casts are no-ops in C and preserve behavior. Verified locally that
#1782 + this change builds cleanly as C++ with g++ (Linux) and MSVC
(Windows).

Refs: #1782
Assisted-by: claude-code:claude-opus-4-8
Comment thread libusb/os/windows_winusb.c Outdated
Comment thread libusb/os/threads_posix.c Outdated
Comment thread libusb/libusbi.h Outdated
Youw added 2 commits June 6, 2026 23:18
- libusbi.h: reword the atomics comment to keep the original Haiku
  context and note the GCC __atomic builtins are used for any GCC C++
  build, so no separate C++23 path is needed.
- os/threads_posix.c: simplify the _Thread_local/thread_local comment.
- os/windows_winusb.{c,h}: fix the WinUsb_ReadIsochPipeAsap_t typedef -
  its first argument was PWINUSB_ISOCH_BUFFER_HANDLE but the WinUSB API
  (and WriteIsochPipeAsap) take WINUSB_ISOCH_BUFFER_HANDLE by value.
  Correcting it drops the void* cast the C++ build otherwise needed.

Refs: #1782
Assisted-by: claude-code:claude-opus-4-8
Add usbi_guid_equal() in windows_common.h, which calls IsEqualGUID and
hides its C-vs-C++ signature difference (pointers in C, const GUID&
references in C++) behind #ifdef __cplusplus, and use it in
windows_winusb.c instead of the earlier raw memcmp. Keeps the intent
clear while still compiling as both C and C++.

Refs: #1782
Assisted-by: claude-code:claude-opus-4-8
@Youw Youw requested a review from seanm June 6, 2026 20:37
@mcuee mcuee added windows linux core Related to common codes labels Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to common codes linux windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants