1

I'm trying to use libusb in a macOS application with GUI. But I always fail with "No such module 'CLibUSB'". I could use CLibUSB in a command line application, but cannot get it to work with a macOS GUI application. What I did:

  1. brew install libusb
  2. $ mkdir Clibusb and $ cd Clibusb/
  3. make a system module

$ swift package init --type system-module

  1. Edit the package manifest Package.swift file to look like this

// swift-tools-version:5.1

import PackageDescription

  let package = Package(
    name: "Clibusb",
    pkgConfig: "libusb-1.0",
    providers: [
      .brew(["libusb"])
    ],
    dependencies: [
    // Dependencies declare other packages that this package depends on
    // .package(url: /* package url */, from: "1.0.0"),
    ]
  )
  1. Create a C shim header,

echo '#include <libusb.h>' >shim.h

  1. Write the module map module.modulemap
  module CLibUSB [system] {
  header "shim.h"
  link "libusb-1.0"
  export *
}
  1. Create a Git repository

git init
git add .
git commit -m "Initial commit"

  1. Make a client app
mkdir LibUSBExample
cd LibUSBExample
swift package init --type executable
  1. In the client app in main.swift. I import CLibUSB and do some tests - all that works

How can I use CLibUsb in a GUI Application?

Fritz
  • 95
  • 7

2 Answers2

0

Download and copy libusb to your project folder from github.

Drag libusb.xcodeproj in libusb/Xcode into your project.

Create bridging header file :

#include "libusb.h"

Update :

Add header search path :

Build Settings

scchn
  • 315
  • 3
  • 16
  • Thanks for your answer, but it looks like I still can't build it. I copied libusb from the GitHub to my project and dragged libusb.xcodeproj to my project - looks like I can build libusb. Next I created the project-bridging-header.h and added the include. But it always gives me "libusb.h file not found" when I try to build my project. – Fritz Aug 23 '21 at 08:36
  • Have you added the header search path? – scchn Aug 24 '21 at 02:02
0

Thanks for all your help!

The bridging header needs to contain #include <libusb-1.0/libusb.h>

I had to set Header Search Path = /usr/local/include and Library Search Path = /usr/local/lib correctly.

Under Frameworks, Library etc. I also had to set the library libusb-1.0.a or libusb-1.0.0.dylib

Fritz
  • 95
  • 7