diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt index c8228bc84..d230c703b 100644 --- a/windows/CMakeLists.txt +++ b/windows/CMakeLists.txt @@ -22,6 +22,12 @@ target_link_libraries(hidapi_winapi PUBLIC hidapi_include ) +target_compile_definitions(hidapi_winapi + # CMake compiles hidapi_descriptor_reconstruct.c as a separate translation unit, + # so hid.c must not #include it (to avoid duplicate symbol LNK4006 in static builds). + PRIVATE HIDAPI_CMAKE_V0_BUILD +) + if(NOT BUILD_SHARED_LIBS) target_compile_definitions(hidapi_winapi # prevent marking functions as __declspec(dllexport) for static library build diff --git a/windows/hid.c b/windows/hid.c index 1e27f10a4..705847e0b 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -1607,7 +1607,9 @@ HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) return last_global_error_str; } -#ifndef hidapi_winapi_EXPORTS +#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