windows: fix LNK4006 duplicate symbol warning in static CMake builds #809

Merged
Copilot merged 2 commits from copilot/fix-cmake-build-warnings into master 2026-05-20 15:24:35 -05:00
Copilot commented 2026-05-19 16:25:45 -05:00 (Migrated from github.com)

When building hidapi as a static library via CMake (e.g. from hidapitester), MSVC emits a linker warning because hidapi_descriptor_reconstruct.c is compiled twice: once as a separate CMake source and once via #include inside hid.c.

The old guard #ifndef hidapi_winapi_EXPORTS was intended to prevent the double-include, but hidapi_winapi_EXPORTS is only set by MSVC for shared (DLL) builds — leaving static builds unprotected.

Changes

  • windows/hid.c: Replace the hidapi_winapi_EXPORTS guard with HIDAPI_CMAKE_V0_BUILD:

    // Before
    #ifndef hidapi_winapi_EXPORTS
    #include "hidapi_descriptor_reconstruct.c"
    #endif
    
    // After
    #ifndef HIDAPI_CMAKE_V0_BUILD
    /* Include the descriptor reconstruction code when NOT building with CMake.
       CMake builds compile hidapi_descriptor_reconstruct.c as a separate translation unit. */
    #include "hidapi_descriptor_reconstruct.c"
    #endif
    
  • windows/CMakeLists.txt: Define HIDAPI_CMAKE_V0_BUILD as a PRIVATE compile definition on hidapi_winapi for all CMake builds (shared and static alike), so hid.c always skips the #include when building via CMake.

Legacy (non-CMake) build systems remain unaffected: without the definition, hid.c continues to #include the file as before.

When building hidapi as a static library via CMake (e.g. from hidapitester), MSVC emits a linker warning because `hidapi_descriptor_reconstruct.c` is compiled twice: once as a separate CMake source and once via `#include` inside `hid.c`. The old guard `#ifndef hidapi_winapi_EXPORTS` was intended to prevent the double-include, but `hidapi_winapi_EXPORTS` is only set by MSVC for shared (DLL) builds — leaving static builds unprotected. ## Changes - **`windows/hid.c`**: Replace the `hidapi_winapi_EXPORTS` guard with `HIDAPI_CMAKE_V0_BUILD`: ```c // Before #ifndef hidapi_winapi_EXPORTS #include "hidapi_descriptor_reconstruct.c" #endif // After #ifndef HIDAPI_CMAKE_V0_BUILD /* Include the descriptor reconstruction code when NOT building with CMake. CMake builds compile hidapi_descriptor_reconstruct.c as a separate translation unit. */ #include "hidapi_descriptor_reconstruct.c" #endif ``` - **`windows/CMakeLists.txt`**: Define `HIDAPI_CMAKE_V0_BUILD` as a `PRIVATE` compile definition on `hidapi_winapi` for all CMake builds (shared and static alike), so `hid.c` always skips the `#include` when building via CMake. Legacy (non-CMake) build systems remain unaffected: without the definition, `hid.c` continues to `#include` the file as before.
Youw (Migrated from github.com) approved these changes 2026-05-19 16:29:27 -05:00
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!809
No description provided.