Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking on Windows #20

Open
TomasHubelbauer opened this issue Oct 23, 2017 · 6 comments
Open

Linking on Windows #20

TomasHubelbauer opened this issue Oct 23, 2017 · 6 comments

Comments

@TomasHubelbauer
Copy link

TomasHubelbauer commented Oct 23, 2017

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.

  • Switched to the GNU based toolchain: rustup override set stable-x86_64-pc-windows-gnu
  • Installed pkg-config-lite
  • Set %PATH% to pkg-config-lite-0.28-1\bin
  • Set %PKG_CONFIG_PATH% to pkg-config-lite-0.28-1\libs I created
  • Verified pkg-config was working using pkg-config -v
  • Donwloaded libusb for Windows by clicking Downloads > Latest Windows binaries
  • Created a libusb.pc file in the PKG_CONFIG_PATH directory:
# 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
Copy link

Boscop commented Nov 3, 2017

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
Copy link
Owner

@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 dcuddeback/libusb-sys#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: dcuddeback/libusb-sys#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 dcuddeback/libusb-sys#5, I'd appreciate some feedback on whether or not it solves your issue.

@critch
Copy link

critch commented Nov 13, 2017

@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
Copy link

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
Copy link

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

@qianchenzhumeng
Copy link

qianchenzhumeng commented Nov 1, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants