cmake: add HIDAPI_BUILD_AS_CXX to compile sources as C++ #814

Merged
Youw merged 1 commit from cmake-build-as-cxx into master 2026-05-21 03:12:01 -05:00
Youw commented 2026-05-21 02:49:44 -05:00 (Migrated from github.com)

What

Add an opt-in CMake variable HIDAPI_BUILD_AS_CXX that wires set_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 via option(), so it doesn't appear in cmake-gui or cmake -L output. It is documented under "Testing-only variables" in BUILD.cmake.md.

# Enable from the command line:
cmake -B build -S . -DHIDAPI_BUILD_AS_CXX=ON \
    "-DCMAKE_CXX_FLAGS=-Wall -Wextra -Werror"
cmake --build build

Wired through every backend: linux, libusb, mac, netbsd, and windows (both hid.c and hidapi_descriptor_reconstruct.c).

Source touches

Three small adjustments to mac/hid.c so Apple Clang accepts the source as C++:

  • cast CFTypeRef to CFNumberRef at the CFNumberGetValue() call (C allows implicit void*-to-pointer conversion; C++ does not);
  • mark BUF_LEN const so the surrounding stack buffer is a non-VLA array (C99 VLAs are a Clang extension under C++);
  • cast calloc()'s return value when assigning to cur_dev->path, matching the cast pattern already used elsewhere in mac/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.yml get a new step that runs a full CMake build with HIDAPI_BUILD_AS_CXX=ON:

  • ubuntu-cmake — exercises hidapi_hidraw + hidapi_libusb under g++.
  • macos-cmake — exercises hidapi_darwin under Apple Clang.
  • windows-cmake — exercises hidapi_winapi under MSVC; also sets -DCMAKE_CXX_STANDARD=20 so the designated initialisers in windows/hid.c / windows/hidapi_descriptor_reconstruct.c parse as ISO C++ (under g++ / Clang's default mode they're a GNU extension; ISO C++ accepted them only since C++20).

-pedantic is intentionally omitted from the C++ check: libusb.h declares 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:

[ 25%] Building CXX object src/linux/CMakeFiles/hidapi_hidraw.dir/hid.c.o
[ 50%] Building CXX object src/libusb/CMakeFiles/hidapi_libusb.dir/hid.c.o
[ 75%] Linking CXX shared library libhidapi-hidraw.so
[100%] Linking CXX shared library libhidapi-libusb.so
build exit=0

CMake reports Building CXX object for the .c sources (because of the LANGUAGE CXX source property), and the produced shared libraries still export the API with C linkage — hidapi.h's existing extern "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.

## What Add an opt-in CMake variable `HIDAPI_BUILD_AS_CXX` that wires `set_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 via `option()`, so it doesn't appear in `cmake-gui` or `cmake -L` output. It is documented under "Testing-only variables" in `BUILD.cmake.md`. ```cmake # Enable from the command line: cmake -B build -S . -DHIDAPI_BUILD_AS_CXX=ON \ "-DCMAKE_CXX_FLAGS=-Wall -Wextra -Werror" cmake --build build ``` Wired through every backend: `linux`, `libusb`, `mac`, `netbsd`, and `windows` (both `hid.c` and `hidapi_descriptor_reconstruct.c`). ## Source touches Three small adjustments to `mac/hid.c` so Apple Clang accepts the source as C++: - cast `CFTypeRef` to `CFNumberRef` at the `CFNumberGetValue()` call (C allows implicit `void*`-to-pointer conversion; C++ does not); - mark `BUF_LEN` `const` so the surrounding stack buffer is a non-VLA array (C99 VLAs are a Clang extension under C++); - cast `calloc()`'s return value when assigning to `cur_dev->path`, matching the cast pattern already used elsewhere in `mac/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.yml` get a new step that runs a full CMake build with `HIDAPI_BUILD_AS_CXX=ON`: - **ubuntu-cmake** — exercises `hidapi_hidraw` + `hidapi_libusb` under g++. - **macos-cmake** — exercises `hidapi_darwin` under Apple Clang. - **windows-cmake** — exercises `hidapi_winapi` under MSVC; also sets `-DCMAKE_CXX_STANDARD=20` so the designated initialisers in `windows/hid.c` / `windows/hidapi_descriptor_reconstruct.c` parse as ISO C++ (under g++ / Clang's default mode they're a GNU extension; ISO C++ accepted them only since C++20). `-pedantic` is intentionally omitted from the C++ check: `libusb.h` declares 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: ``` [ 25%] Building CXX object src/linux/CMakeFiles/hidapi_hidraw.dir/hid.c.o [ 50%] Building CXX object src/libusb/CMakeFiles/hidapi_libusb.dir/hid.c.o [ 75%] Linking CXX shared library libhidapi-hidraw.so [100%] Linking CXX shared library libhidapi-libusb.so build exit=0 ``` CMake reports `Building CXX object` for the `.c` sources (because of the `LANGUAGE CXX` source property), and the produced shared libraries still export the API with C linkage — `hidapi.h`'s existing `extern "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._
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-libusb/hidapi!814
No description provided.