windows: fix LNK4006 duplicate symbol warning in static CMake builds#809
Merged
Conversation
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
Youw
approved these changes
May 19, 2026
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.
When building hidapi as a static library via CMake (e.g. from hidapitester), MSVC emits a linker warning because
hidapi_descriptor_reconstruct.cis compiled twice: once as a separate CMake source and once via#includeinsidehid.c.The old guard
#ifndef hidapi_winapi_EXPORTSwas intended to prevent the double-include, buthidapi_winapi_EXPORTSis only set by MSVC for shared (DLL) builds — leaving static builds unprotected.Changes
windows/hid.c: Replace thehidapi_winapi_EXPORTSguard withHIDAPI_CMAKE_V0_BUILD:windows/CMakeLists.txt: DefineHIDAPI_CMAKE_V0_BUILDas aPRIVATEcompile definition onhidapi_winapifor all CMake builds (shared and static alike), sohid.calways skips the#includewhen building via CMake.Legacy (non-CMake) build systems remain unaffected: without the definition,
hid.ccontinues to#includethe file as before.