Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use libusb in a MacOS GUI application

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?

like image 664
Fritz Avatar asked Jul 23 '26 01:07

Fritz


1 Answers

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

like image 182
scchn Avatar answered Jul 25 '26 17:07

scchn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!