cmake: add HIDAPI_BUILD_AS_CXX to compile sources as C++#814
Merged
Conversation
Add an opt-in CMake variable HIDAPI_BUILD_AS_CXX that, when set to
TRUE, attaches LANGUAGE CXX to each backend's C source file so it is
compiled with the 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 intentionally not declared as a CMake option(), so it
is not surfaced in cmake-gui or `cmake -L` output. It is documented
under "Testing-only variables" in BUILD.cmake.md.
Wire it through every backend (linux, libusb, mac, netbsd, windows).
For the windows backend, both hid.c and hidapi_descriptor_reconstruct.c
are re-tagged.
Three small mac/hid.c adjustments are required for Apple Clang to
accept the source as C++:
- cast CFTypeRef to CFNumberRef at CFNumberGetValue() (C allows
implicit void*-to-pointer conversion; C++ does not);
- mark BUF_LEN const so the stack buffer is a non-VLA array;
- cast calloc()'s return value when assigning to cur_dev->path.
CI: enable HIDAPI_BUILD_AS_CXX=ON on the ubuntu-cmake, macos-cmake
and windows-cmake jobs to guard against regressions. The MSVC variant
also sets CMAKE_CXX_STANDARD=20 so the designated initialisers in the
Windows backend compile as ISO C++ (g++ and Clang accept them as a
GNU extension in their default mode).
Assisted-by: Claude:claude-opus-4.7
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.
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.