Skip to content

Fix build on macOS Apple Silicon (darwin-aarch64)#18

Open
axgd-code wants to merge 1 commit intousb4java:masterfrom
axgd-code:fix/darwin-aarch64-macos-frameworks
Open

Fix build on macOS Apple Silicon (darwin-aarch64)#18
axgd-code wants to merge 1 commit intousb4java:masterfrom
axgd-code:fix/darwin-aarch64-macos-frameworks

Conversation

@axgd-code
Copy link
Copy Markdown

Problem

Building libusb4java on macOS Apple Silicon (darwin-aarch64) fails at the link step with undefined symbols:

Undefined symbols for architecture arm64:
  _IOCreatePlugInInterfaceForService, referenced from:
      _usbi_backend in libusb-1.0.a(darwin_usb.c.o)
  _kIOMasterPortDefault, referenced from:
      _usbi_backend in libusb-1.0.a(darwin_usb.c.o)
  _objc_msgSend, referenced from:
      _usbi_backend in libusb-1.0.a(darwin_usb.c.o)
  ...
ld: symbol(s) not found for architecture arm64

This happens because the build script (dists/darwin/build) links libusb statically. The static libusb-1.0.a on macOS depends on Apple system frameworks (CoreFoundation, IOKit, Security) and the Objective-C runtime (objc), which are not automatically pulled in by CMake.

This issue was first reported in #12 and is also tracked upstream at usb4java/usb4java#86. It affects all Apple Silicon Macs (M1/M2/M3/M4) running macOS. Related discussion: marian-m12l/studio#244.

Fix

Add an APPLE-guarded block in src/CMakeLists.txt that links the required macOS system frameworks:

if(APPLE)
  target_link_libraries(usb4java
    "-framework CoreFoundation"
    "-framework IOKit"
    "-framework Security"
    "-lobjc"
  )
endif()

This is the standard CMake idiom for linking Apple frameworks. The block is gated on APPLE so it has no effect on Linux or Windows builds.

Verification

Verified on:

  • macOS 15 Sequoia, Apple M3 (darwin-aarch64)
  • Temurin JDK 21, libusb installed via Homebrew
  • The produced libusb4java.dylib loads correctly via JNI and LibUsb.init() returns 0

The resulting darwin-aarch64 JAR has been tested with TomTomWatch connecting to a physical TomTom GPS watch over USB.

When building libusb4java with a statically linked libusb on macOS, the
linker fails with undefined symbols for CoreFoundation, IOKit, Security and
objc because libusb itself depends on those Apple frameworks.

Add an APPLE-guarded block in src/CMakeLists.txt that links those four
system frameworks when building on macOS:

  if(APPLE)
    target_link_libraries(usb4java
      "-framework CoreFoundation"
      "-framework IOKit"
      "-framework Security"
      "-lobjc"
    )
  endif()

Without this change the build script (dists/darwin/build) fails at the link
step with errors like:
  Undefined symbols for architecture arm64:
    _IOCreatePlugInInterfaceForService, referenced from ...
    _kIOMasterPortDefault, referenced from ...

Verified on macOS 15 (Sequoia) / Apple M3 with Temurin JDK 21.
Fixes usb4java#12 and closes usb4java/usb4java#86.
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

Successfully merging this pull request may close these issues.

1 participant