Make the Linux and Windows backends compile as C++#1848
Open
Youw wants to merge 3 commits into
Open
Conversation
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
Youw
commented
Jun 6, 2026
Youw
commented
Jun 6, 2026
Youw
commented
Jun 6, 2026
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_Atomickeyword); this was by far the biggest blocker on Linux (>400 cascading errors). Use the GCC__atomicbuiltins for any GCC C++ build (previously only the Haiku path did). C and clang builds are unchanged.os/threads_posix.c— map C11_Thread_localtothread_localunder__cplusplus(g++ rejects_Thread_localin C++).os/linux_usbfs.c— explicit casts onvoid*allocations andusbi_get_*_priv()accessors; cast aninttransfer status to its enum; reorder theusbi_os_backendinitializer to the struct's declaration order (C++ requires designated initializers in order).windows_common.{c,h},windows_winusb.{c,h},windows_usbdk.c,events_windows.c) — explicit casts onvoid*allocations/accessors; compare GUIDs withmemcmp(IsEqualGUIDtakes references in C++); one enum→uint8_tnarrowing cast for/WX.All casts are no-ops in C and preserve behavior.
Verification (reproduced locally on Windows + WSL)
-std=c++20): the.sobuilds cleanly./TP,CompileAs=CompileAsCpp): the DLL builds cleanly (0 warnings under/WX).The C++-build CI proposed in #1845 exercises exactly this.
Refs: #1782
Assisted-by: claude-code:claude-opus-4-8