Windows平台Visual Studio 2019(MSVC)下Libusb动态链接库(DLL)损坏导致链接错误求助
Hey Leo, let's work through that frustrating LNK1107 error you're facing when trying to use dynamic linking with Libusb in VS2019. As someone who's hopped between UNIX and Windows development, I know how easy it is to trip up on Windows' linking rules. Here are targeted fixes to get your dynamic build working:
1. Stop linking directly to the .dll file (use the import .lib instead)
The most common cause of this error is trying to add the libusb-1.0.dll directly to your linker's additional dependencies. MSVC requires an import library (.lib) to link against dynamic DLLs—you can't use the DLL file itself for linking.
- Go to your project properties → Linker → Input → Additional Dependencies
- Replace any reference to
libusb-1.0.dllwithlibusb-1.0.lib(this is the import library generated when you compiled Libusb's dynamic version) - Make sure this
.libfile is from the samex64 Debugbuild as your project.
2. Match architecture and build type exactly
Windows is strict about matching 32-bit/64-bit and Debug/Release builds between your library and project. Mismatches often lead to "corrupted file" errors:
- Double-check your project's configuration: Go to Build → Configuration Manager and confirm it's set to
x64 Debug - Grab the correct files from your Libusb build output:
- Import lib:
libusb-1.0.24\msvc\x64\Debug\libusb-1.0.lib - DLL (for runtime):
libusb-1.0.24\msvc\x64\Debug\libusb-1.0.dll
- Import lib:
- Copy the DLL to your project's output directory (usually
your-project\x64\Debug) so it's found at runtime.
3. Clean and rebuild everything to eliminate stale cache
Sometimes leftover build artifacts from static linking attempts can cause conflicts. A full clean ensures you're using fresh, matching files:
- Open the Libusb VS2019 solution, select
x64 Debug, right-click the project → Clean, then Rebuild - Do the same for your own project: Right-click → Clean, then Rebuild
4. Ensure the LIBUSB_SHARED macro is defined
For dynamic linking to work correctly, both Libusb and your project need to define the LIBUSB_SHARED preprocessor macro. This tells the compiler to generate code compatible with dynamic libraries:
- In your project properties → C/C++ → Preprocessor → Preprocessor Definitions, add
LIBUSB_SHARED(if it's not already there) - Verify Libusb's dynamic build has this macro enabled (it should be set by default in the VS project for dynamic configurations)
5. Avoid third-party DLLs—stick to your compiled version
DLLs from random third-party sites are risky: they might be outdated, modified, or compiled with different settings that clash with your environment. Always use the DLL you compiled yourself from the official Libusb source—it's the only way to guarantee compatibility.
Give these steps a shot, and let me know if any of them resolve your issue. If you're still stuck, sharing a screenshot of your linker settings or the exact paths you're using for the library files could help narrow things down further.
内容的提问来源于stack exchange,提问作者leoK

