dcuddeback / libusb-rs

A safe Rust wrapper for libusb.
MIT License
200 stars 65 forks source link

Linking on Windows #20

Open TomasHubelbauer opened 7 years ago

TomasHubelbauer commented 7 years ago

Seeing @bitbegin's issue (#17), I tried to use the GNU toolchain, but ended up with an error.

I'm on Windows 10 Home 64 bit.

# libusb.pc

prefix=C:/Users/Tom/Desktop/pkg-config-lite-0.28-1/libs/libusb
exec_prefix=${prefix}
includedir=${prefix}/include/libusb-1.0
libdir=${exec_prefix}/MinGW64/dll # `/dll`?

Name: libusb
Description: libusb
Version: 1.0
Cflags: -I${includedir}
Libs: -L${libdir} -llibusb-1.0

Here's what pkg-config --libs --cflags libusb-1.0 spits out for me: -IC:/Users/Tom/Desktop/pkg-config-lite-0.28-1/libs/libusb/include/libusb-1.0 -LC:/Users/Tom/Desktop/pkg-config-lite-0.28-1/libs/libusb/MinGW64/dll -llibusb-1.0

I am using this example code based on what's in the README.md file of this repository:

extern crate libusb;

fn main() {
    let context = libusb::Context::new().unwrap();
    for device in context.devices().unwrap().iter() {
        let device_desc = device.device_descriptor().unwrap();

        println!("Bus {:03} Device {:03} ID {:04x}:{:04x}",
            device.bus_number(),
            device.address(),
            device_desc.vendor_id(),
            device_desc.product_id());
    }

    println!("Hello, world!");
}

Running cargo run then links just fine and runs the binary which subsequently fails with the 3221225781 exit code, which is 0xC0000135, which means STATUS_DLL_NOT_FOUND.

I suspect this is because I am using the dll directory, not the static directory, but with that one, I just get linked errors.

@dcuddeback would you be able to advise here?

Boscop commented 7 years ago

I got it working on windows with a .cargo/config build script override and the dll from here: https://sourceforge.net/projects/libusb/files/

[target.x86_64-pc-windows-msvc.'usb-1.0']
rustc-link-search = ['D:\libs\64bit']
rustc-link-lib = ['libusb-1.0']
dcuddeback commented 7 years ago

@TomasHubelbauer When I built this library (March 2015), rustup didn't exist, so I've not tested with the toolchains provided by rustup. The documented way to install Rust on Windows at that time was to use MSYS2. I documented the steps that worked for me in https://github.com/dcuddeback/libusb-sys/issues/4. There's also a PR for libusb-sys that borrows a build strategy from rust-openssl, which is supposed to work with MSVC as well: https://github.com/dcuddeback/libusb-sys/pull/5. You may want to give that a try. I haven't had time to test it myself. I'm not as comfortable on Windows as I am on Unix, so I could use help solving Windows build issues. If you happen to try https://github.com/dcuddeback/libusb-sys/pull/5, I'd appreciate some feedback on whether or not it solves your issue.

critch commented 7 years ago

@TomasHubelbauer Thank you for your detailed report. This report helped me get my instance compiling with libusb. Specifically the pkg-config portion helped me the most.

TypedLambda commented 6 years ago

I just succesfully built a windows binary using dcuddeback/libusb-sys#5. if you want to build a project depending on libusb-rs, here are the steps required:

Cargo.toml
[replace]
"libusb-sys:0.2.3"= { git = "https://github.com/cmsd2/libusb-sys/" }

then export LIBUSB_DIR to point to the libusb installation. I installed libusb using vcpkg on windows. and pointed to the installed package folder for libusb. Tested on Windows 10 and FreeBSD for now.

The change provides a very nice and helpfull error message if libusb is not found. +1 for merging from here...

LorenzoTesta commented 5 years ago

@TomasHubelbauer Thank you for your detailed report!! the best libusb/windows docs I've found! Thank you!

qianchenzhumeng commented 3 years ago

@TomasHubelbauer, use the static lib, and modify the last line of libusb.pc to:

Libs: -L${libdir} -lusb-1.0

There is my libusb.pc:

prefix=C:\MinGW
exec_prefix=${prefix}
includedir=${prefix}/include/libusb-1.0
libdir=${exec_prefix}/lib/libusb

Name: libusb
Description: libusb
Version: 1.0
Cflags: -I${includedir}
Libs: -L${libdir} -lusb-1.0

libdir has only one file that is a copy of libusb-1.0.24.7z\MinGW64\static\libusb-1.0.a.