macOS Command Line Tool with Swift Cocoa Framework: Library not loaded
Swift Package Manager sets the following, which resolved this issue for me in another project:
SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES
SWIFT_FORCE_STATIC_LINK_STDLIB = NO
Note that the search path was set to:
LD_RUNPATH_SEARCH_PATHS = $(TOOLCHAIN_DIR)/usr/lib/swift/macosx @executable_path
Setting up a Framework on macOS Command Line apps - Reason: image not found
Commandline tools do indeed "support" Frameworks, but not in the way Application bundles do. You need to put the referenced frameworks in the @rpath
which in practice is ~/Library/Frameworks/
or /Library/Frameworks/
Can't add dynamic frameworks to Command Line Tool
Command-line tools are best with static archives because everything is distributed as a single binary. Looking at Realm, I don't see that there is a static archive option. They do have an iOS static framework that I got compiling for macOS but that's not quite what you want. You might want to try playing with Realm's source a bit more to see if you can get it to produce a static archive.
In the mean time, as a workaround, you'll need to tell Xcode where to find the dylibs at runtime and also to install them somewhere.
- In your Build Settings, go down to "Runpath Search Paths" and add "
@rpath
". - In Build Phases, under Copy Files, click the + button and add both Realm.framework and RealmSwift.framework from your project.
- Because Realm is compiled with an older version of Swift, you also need to specify "Use Legacy Swift Language Version" in Build Settings.
That will get your project building and finding the Realm libraries but now it will fail to find libswiftCore.dylib. That's because normally command-line tools are statically linked with the Swift library but as soon as you add a framework/dylib, the linker no longer includes the static version.
- Go back to Build Phases, Copy Files, and add the following:
libswiftObjectiveC.dylib
libswiftIOKit.dylib
libswiftFoundation.dylib
libswiftDispatch.dylib
libswiftDarwin.dylib
libswiftCoreGraphics.dylib
libswiftCore.dylib
You can find them inside your Xcode installation and then ./Contents/Developer/Toolchains/Swift_2.3.xctoolchain/usr/lib/swift/macosx/
WARNING: Keep in mind that you will need to distribute the frameworks and the dylibs with your command-line tool and they will need to be in the same directory as the tool. You can put them somewhere else on the system by specifying a different runpath but you'll still need them distributed with your tool.
The nice thing about a .app bundle is that it gives you a place to put this stuff and users can just drag-and-drop it to install it. If you could get a static archive version of Realm, you could distribute everything in one binary.
iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta
In the target's General tab, there is an Embedded Binaries field. When you add the framework there the crash is resolved.
Reference is here on Apple Developer Forums.
Xcode - dyld: Library not loaded
Patch your Podfile according to:
https://github.com/CocoaPods/CocoaPods/issues/3707
The version I use for my hello world CLI app:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['DYLIB_INSTALL_NAME_BASE'] = target.product_name
config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = 'YES'
end
end
end
It is more a workaround than a solution.
Related Topics
Xcode 11 Beta 3, Build Error "Unknown Attribute 'State'", "Use of Undeclared Type 'View'" etc
Use of Undeclared Type 'Attributedstring'
Swift 2.0 'Inout' Function Parameters and Computed Properties
What Makes a Property a Computed Property in Swift
How to Embed Third Party Framework on Ionic Capacitor Custom Plugin
How to Speed Up Updating Relationship Among Tables, After One or Both Tables Are Already Saved
How to Demonstrate a Zombie Object in Swift
How to Read a Property List from Data in Swift 3
Suppressing Implicit Returns in Swift
How to Implement Protocol Methods That Return Covariant Selfs
How to Make Uitextfield Behave Like a Uisearchbar in Swift
Read and Write Permission for User Selected Folder in MAC Os App
Save Arfacegeometry to Obj File