Pre: Please install mingw64. This will ship with GCC which will be required by golang compiler to build gousb.
Steps -
- Clone gousb in your project by running
git clone git@github.com:google/gousb - One necessity to get it running is to install libusb on windows; which is not hard but gousb wants you to install the dev header as well with a pkg-config (.pc) file. To get past it, we can just download the package as it is by going to MSYS2 Packages [2] and searching for libusb. You'll end up on this package [3] that has the archive which mingw installs. Just click on the download link in the second section.
- Use an un-archiving utility to un-archive this package and paste it in your project.
- Now edit the line which invokes pkg-config in gousb. It is located in gosub/libusb.go. Change
#cgo pkg-config: libusb-1.0to the expected output of pkg-config which is the CFLAGS and LDFLAGS. Something like –
#cgo CFLAGS: -I../mingw64/include/libusb-1.0 (relative path to the include folder of the downloaded package)
#cgo LDFLAGS: -L../mingw64/bin/ (relative path to the bin folder of the downloaded package)
#cgo LDFLAGS: -lusb-1.0 (copied from the .pc file that is located in the downloaded package)- You might have to update the
#inlcudeline to the relative path as well in all the files. Just search for#includeand update all instances. - Now to make it work, you must tell your
go.modfile to use the local package of gousb instead of the official one. You can do that by just adding a replace line in your mod file like –replace github.com/google/gousb => ./<relative-path-to-edited-gousb-project/s.
And you should be good to go.
Cya. 🫡
References
[1] - https://github.com/google/gousb/blob/2f9ed92cb863c87f7bb5fa58a54c6502ef0a5e1f/libusb.go#L27
[2] - https://packages.msys2.org
[3] - https://packages.msys2.org/package/mingw-w64-x86_64-libusb?repo=mingw64