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

感情迁移 提交于 2019-12-01 20:43:14

@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" }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!