Skip to content

Link IOKit and CoreFoundation into the shared library on macOS - #368

Merged
whoozle merged 2 commits into
whoozle:masterfrom
jinwukong:fix-macos-shared-library
Oct 8, 2026
Merged

whoozle merged 2 commits into
whoozle:masterfrom
jinwukong:fix-macos-shared-library

Conversation

@jinwukong

@jinwukong jinwukong commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Building with -DBUILD_SHARED_LIB=ON on macOS fails at link time:

Undefined symbols for architecture arm64:
  "_CFUUIDGetConstantUUIDWithBytes", referenced from:
      mtp::usb::Configuration::Configuration(...) in DeviceDescriptor.cpp.o
  "_CFUUIDGetUUIDBytes", referenced from: ...
  "_IOCreatePlugInInterfaceForService", referenced from: ...

The Darwin backend appends IOKit and CoreFoundation to MTP_LIBRARIES only, which the shared library target does not link (MTP_SHARED_LIBRARIES is used there), so libmtp-ng.dylib is left with unresolved framework symbols. The static library path gets the frameworks through MTP_LIBRARIES, which is why the default build works.

This appends the two frameworks to MTP_SHARED_LIBRARIES as well, mirroring the libusb branch that already links its library into the shared target.

Verified on macOS 27 (arm64) with -DBUILD_SHARED_LIB=ON -DBUILD_QT_UI=OFF -DBUILD_FUSE=OFF -DBUILD_MTPZ=OFF -DBUILD_PYTHON=OFF -DBUILD_TAGLIB=OFF: libmtp-ng.dylib links, aft-mtp-cli links against it and runs.

Second commit: after installing, aft-mtp-cli from the shared build still could not start (dyld: Library not loaded: @rpath/libmtp-ng.dylib) because CMake drops the build tree rpath on install and nothing sets CMAKE_INSTALL_RPATH. It now defaults to @loader_path/../lib${LIB_SUFFIX} on macOS for the shared build when the packager has not set one, so a plain cmake --install gives a working binary. The default static build is unchanged.

With -DBUILD_SHARED_LIB=ON the Darwin USB backend's IOKit and
CoreFoundation frameworks were only appended to MTP_LIBRARIES, which the
shared library target does not use, so linking libmtp-ng.dylib failed
with undefined symbols (_CFUUIDGetConstantUUIDWithBytes, _IOCreatePlugInInterfaceForService, ...).
Append them to MTP_SHARED_LIBRARIES as well, mirroring what the static
library already gets.
CMake strips the build tree rpath when installing, and nothing set
CMAKE_INSTALL_RPATH, so an installed aft-mtp-cli built with
-DBUILD_SHARED_LIB=ON could not load @rpath/libmtp-ng.dylib. Default it to
@loader_path/../lib on macOS unless the packager already set one.
@jinwukong

Copy link
Copy Markdown
Contributor Author

Some context on why this matters beyond the build itself: I am adding aft-mtp-cli to Homebrew (Homebrew/homebrew-core#316024). Homebrew requires new formulae to build the shared library and does not accept the fix as a downstream patch, so the formula is waiting on this change landing in a tagged release.

If the change looks right to you, a release that includes it (master has carried the 4.6 version bump since May 2025) would let Homebrew pick it up. Happy to adjust anything in this PR.

@whoozle
whoozle merged commit 712dce6 into whoozle:master Oct 8, 2026
@jinwukong

jinwukong commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for merging! One follow-up ask: would you tag a v4.6 release when convenient? master has carried the 4.6 bump since May 2025, and the tag workflow already builds the AppImage and DMG.

The concrete blocker on my side is the Homebrew formula (Homebrew/homebrew-core#316024): the maintainers there require new formulae to build the shared library and do not take patches, so their verdict was "it will need to wait for a release with a fix either way". A stable tag with this change is the only thing standing between that PR and being merged. No rush beyond that, and thanks again for the quick merge.

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.

2 participants