Rust program requires the libusb DLL to be present even though it was statically linked

后端 未结 1 1564
面向向阳花
面向向阳花 2021-01-20 16:04

I\'m trying to make a Rust program that statically links against libusb, using the MSVC toolchain, but it blows up at run-time from a missing DLL:



        
1条回答
  •  时光说笑
    2021-01-20 16:47

    @Shepmaster was right, the libusb:x64-windows triplet for vcpkg only contains definitions that try to call the real functions from the DLL.

    I tried to load this library from a simple C program and I got the exact same error:

    test.c

    #include "C:\vcpkg\installed\x64-windows\include\libusb-1.0\libusb.h"
    
    int main()
    {
        const struct libusb_version *version = libusb_get_version();
        printf("Version %d.%d.%d", version->major, version->minor, version->micro);
    }
    

    compiled with:

    cl test.c /link C:\vcpkg\installed\x64-windows\lib\libusb-1.0.lib
    

    results in the same missing DLL error. But if I do with with the different libusb:x64-windows-static triplet:

    test.c

    #pragma comment(lib, "Advapi32.lib")
    #include "C:\vcpkg\installed\x64-windows-static\include\libusb-1.0\libusb.h"
    
    int main()
    {
        const struct libusb_version *version = libusb_get_version();
        printf("Version %d.%d.%d", version->major, version->minor, version->micro);
    }
    

    compiled with:

    cl test.c /link C:\vcpkg\installed\x64-windows-static\lib\libusb-1.0.lib
    

    works just fine:

    >test.exe
    Version 1.0.22
    

    To sum it up, if you want to statically link a Rust program against libusb, download vspkg and install vcpkg.exe install libusb:x64-windows-static. Set an environment variable LIBUSB_DIR that points to C:\vcpkg\installed\x64-windows-static and use the patched version of libusb-sys by putting this in your

    Cargo.toml:

    [dependencies]
    libusb = "0.3"
    libusb-sys = "0.2.3"
    
    [patch.crates-io]
    "libusb-sys" = { git = "https://github.com/cmsd2/libusb-sys" }
    

    0 讨论(0)
提交回复
热议问题