cmake: add HIDAPI_BUILD_AS_CXX to compile sources as C++ #814
No reviewers
Labels
No labels
3rdparty
Android
API
Author does not respond
bsd
bug
build system/CI
ci-virtual-device
CMake
Contributions Welcome
Core
dependencies
device-specific
documentation
don't_merge
duplicate
enhancement
Example
Haiku
hidraw
hotplug
info required
invalid
libusb
macOS
Misc
Not an issue
question
Release
Test
Test GUI
Windows
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libusb/hidapi!814
Loading…
Reference in a new issue
No description provided.
Delete branch "cmake-build-as-cxx"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Add an opt-in CMake variable
HIDAPI_BUILD_AS_CXXthat wiresset_source_files_properties(... LANGUAGE CXX)on every backend's C source file, so the file is compiled with the project's C++ compiler instead of the C compiler. Useful as a portability check — to confirm hidapi sources remain compatible with consumers that embed them into a C++ translation unit.The variable is declared as a plain
if(NOT DEFINED ...)default rather than viaoption(), so it doesn't appear incmake-guiorcmake -Loutput. It is documented under "Testing-only variables" inBUILD.cmake.md.Wired through every backend:
linux,libusb,mac,netbsd, andwindows(bothhid.candhidapi_descriptor_reconstruct.c).Source touches
Three small adjustments to
mac/hid.cso Apple Clang accepts the source as C++:CFTypeReftoCFNumberRefat theCFNumberGetValue()call (C allows implicitvoid*-to-pointer conversion; C++ does not);BUF_LENconstso the surrounding stack buffer is a non-VLA array (C99 VLAs are a Clang extension under C++);calloc()'s return value when assigning tocur_dev->path, matching the cast pattern already used elsewhere inmac/hid.c.Linux, libusb (post-#811) and the Windows backend already compile cleanly as C++; no source changes were needed there.
CI
To guard the option against regressions, three CI jobs in
builds.ymlget a new step that runs a full CMake build withHIDAPI_BUILD_AS_CXX=ON:hidapi_hidraw+hidapi_libusbunder g++.hidapi_darwinunder Apple Clang.hidapi_winapiunder MSVC; also sets-DCMAKE_CXX_STANDARD=20so the designated initialisers inwindows/hid.c/windows/hidapi_descriptor_reconstruct.cparse as ISO C++ (under g++ / Clang's default mode they're a GNU extension; ISO C++ accepted them only since C++20).-pedanticis intentionally omitted from the C++ check:libusb.hdeclares zero-size arrays, which g++ rejects under strict ISO C++ regardless of how hidapi itself is written. The existing C-build CI steps continue to test with-pedantic.Local verification
Tested in WSL Ubuntu (gcc/g++ 11.4) — both Linux-side backends compile cleanly:
CMake reports
Building CXX objectfor the.csources (because of theLANGUAGE CXXsource property), and the produced shared libraries still export the API with C linkage —hidapi.h's existingextern "C"wrapper handles that — so the option doesn't break consumers if it's accidentally turned on. The mac and Windows-MSVC paths are covered by CI.Drafted with Claude Code.