Archived mac app crashed with SwiftPM wrapped system library dependency

I use SwiftPM to wrapper the Tesseract C library by official guide requiring-system-libraries. And it works on my mac device. However, the archived App crash on the other device without the Tesseract installation.

I notice the SwiftPM does not contain dylib so the Xcode does not embed the libraries. After adding the liblept.dylib and libtesseract.dylib under the /usr/local/lib/ to Xcode - App Target - "Frameworks, Libraries, and Embedded Content" section. The app size looks normal but still have that error message on other people device.

Termination Reason: DYLD, [0x1] Library missing

Application Specific Information:
dyld: launch, loading dependent libraries

Dyld Error Message:
Library not loaded: /usr/local/opt/tesseract/lib/libtesseract.4.dylib
Referenced from: /Users/USER/Downloads/ImageReader.app/Contents/MacOS/ImageReader
Reason: image not found

The dylib is already embedded under the ./Content/Frameworks/. How to make the app search and use it?

Thanks.

Demo:

You need to use the install_name_tool (probably in a post-build runscript) to change the dyld path embedded in ImageReader during the build phase to using rpath which would cause the dyld linker at runtime find the dylibs. Man pages on dyld and install_name_tool explain the concept.

If you run otool on your executable (ImageReader), you will list the paths of the dylibs that have been linked into the image. I thinks it is either otool -L or otool -D.

1 Like

I have read the install_name_tool manual and Apple Dynamic Library Programming Topics document. That's very helpful.

Finally I use the Xcode Run Script to set up the dylib loader. The archive application running without issue now.

set -ex

# redirect
install_name_tool -change /usr/local/opt/openjpeg/lib/libopenjp2.7.dylib @rpath/libopenjp2.2.3.1.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/webp/lib/libwebp.7.dylib @rpath/libwebp.7.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/webp/lib/libwebpmux.3.dylib @rpath/libwebpmux.3.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/libtiff/lib/libtiff.5.dylib @rpath/libtiff.5.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/giflib/lib/libgif.dylib @rpath/libgif.7.2.0.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/jpeg/lib/libjpeg.9.dylib @rpath/libjpeg.9.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/libpng/lib/libpng16.16.dylib @rpath/libpng16.16.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/leptonica/lib/liblept.5.dylib @rpath/liblept.5.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"
install_name_tool -change /usr/local/opt/tesseract/lib/libtesseract.4.dylib @rpath/libtesseract.4.dylib "$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH"


# libwebpmux
install_name_tool -change /usr/local/Cellar/webp/1.1.0/lib/libwebp.7.dylib @rpath/libwebp.7.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libwebpmux.3.dylib

# libtiff
install_name_tool -change /usr/local/opt/jpeg/lib/libjpeg.9.dylib @rpath/libjpeg.9.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libtiff.5.dylib

# liblept
install_name_tool -change /usr/local/opt/openjpeg/lib/libopenjp2.7.dylib @rpath/libopenjp2.2.3.1.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib
install_name_tool -change /usr/local/opt/webp/lib/libwebp.7.dylib @rpath/libwebp.7.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib
install_name_tool -change /usr/local/opt/webp/lib/libwebpmux.3.dylib @rpath/libwebpmux.3.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib
install_name_tool -change /usr/local/opt/libtiff/lib/libtiff.5.dylib @rpath/libtiff.5.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib
install_name_tool -change /usr/local/opt/giflib/lib/libgif.dylib @rpath/libgif.7.2.0.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib
install_name_tool -change /usr/local/opt/jpeg/lib/libjpeg.9.dylib @rpath/libjpeg.9.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib
install_name_tool -change /usr/local/opt/libpng/lib/libpng16.16.dylib @rpath/libpng16.16.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/liblept.5.dylib

# libtesseract
install_name_tool -change /usr/local/opt/leptonica/lib/liblept.5.dylib @rpath/liblept.5.dylib $BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/libtesseract.4.dylib

Thank you for your help.

Terms of Service

Privacy Policy

Cookie Policy