skip to content
Nikhil

Setting up gousb on Windows without pkg-config and Cygwin

/ 2 min read

Steps for setting up gousb

  1. Clone gousb in your project by running git clone git@github.com:google/gousb
  2. 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.
  3. Use an un-archiving utility to un-archive this package and paste it in your project.
  4. Now edit the line which invokes pkg-config in gousb. It is located in gosub/libusb.go. Change #cgo pkg-config: libusb-1.0 to the expected output of pkg-config which is the CFLAGS and LDFLAGS. Something like –
gosub/libusb.go
#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)
  1. You might have to update the #inlcude line to the relative path as well in all the files. Just search for #include and update all instances.
  2. Now to make it work, you must tell your go.mod file 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.