Skip to content

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

Merged
Youw merged 2 commits into
masterfrom
copilot/fix-cmake-build-warnings
May 20, 2026
Merged

windows: fix LNK4006 duplicate symbol warning in static CMake builds#809
Youw merged 2 commits into
masterfrom
copilot/fix-cmake-build-warnings

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 19, 2026

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.

Copilot AI linked an issue May 19, 2026 that may be closed by this pull request
Replace `#ifndef hidapi_winapi_EXPORTS` guard in windows/hid.c with
`#ifndef HIDAPI_CMAKE_V0_BUILD`, and define HIDAPI_CMAKE_V0_BUILD as a
PRIVATE compile definition on the hidapi_winapi CMake target.

When building with CMake, hidapi_descriptor_reconstruct.c is compiled as
a separate translation unit. The old guard relied on hidapi_winapi_EXPORTS
which is only set for shared (DLL) builds, causing the file to be compiled
twice in static builds and producing a benign-but-noisy LNK4006 warning.

The new HIDAPI_CMAKE_V0_BUILD define is set for all CMake builds (both
shared and static), so hid.c never #includes the .c file when using CMake.
Legacy build systems that don't define HIDAPI_CMAKE_V0_BUILD continue to
work as before because hid.c still #includes the file in that case.

Agent-Logs-Url: https://github.com/libusb/hidapi/sessions/a624aff3-07ad-401b-8587-87bc5203841b

Co-authored-by: Youw <5939659+Youw@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix warnings when building hidapitester using CMake windows: fix LNK4006 duplicate symbol warning in static CMake builds May 19, 2026
Copilot finished work on behalf of Youw May 19, 2026 21:29
Copilot AI requested a review from Youw May 19, 2026 21:29
@Youw Youw marked this pull request as ready for review May 19, 2026 21:30
@mcuee mcuee added Windows Related to Windows backend CMake Related to CMake build system labels May 20, 2026
@Youw Youw merged commit 78f95a3 into master May 20, 2026
23 checks passed
@Youw Youw deleted the copilot/fix-cmake-build-warnings branch May 20, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CMake Related to CMake build system Windows Related to Windows backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warnings when build hidapitester using CMake

3 participants