Problem with Frameworks in Command Line Tool

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.

  1. In your Build Settings, go down to "Runpath Search Paths" and add "@rpath".
  2. In Build Phases, under Copy Files, click the + button and add both Realm.framework and RealmSwift.framework from your project.
  3. 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.


  1. 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.

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/

Build error while trying to build a Swift-based command line tool depending on a third-party framework: this target might include its own product

The only way I managed to build a command line tool using a third-party Swift libraries is to use Swift Package Manager (SPM).

Example of Swift project using SPM (that could be generated using swift package init --type executable):

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: “mytool”,
dependencies: [
.package(url: "https://github.com/myfreeweb/SwiftCBOR.git", .branch("master")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: “mytool”,
dependencies: [ "SwiftCBOR" ]),
]
)

A Xcode project could be generated from this SPM using: swift package generate-xcodeproj

How to consume a Swift framework or library from OS X Command Line Tool

I finally managed to find a blog that lists exactly how to do this. The downside is that the resultant build products are a bundle (the instructions also work if you replace bundle with GUI app), with the command line tool's executable in the bundle/app's Contents/MacOS folder. So the user can not just call ./BundleName.bundle or ./AppName.app and have the command line tool run, they have to do ./BundleName.bundle/Contents/MacOS/CommandLineTool.

I'm hesitant to actually use a bundle as I feel most people will not understand what to do with it, and since it's the same effort to embed in a GUI app that's the route I'm leaning towards. Currently I won't be adding any functionality to the app, but I could at least add a label to show usage instructions for the command line tool.

The only other option I've found is to make the framework like normal, but also set the target membership of all the framework's Swift files to also include them in the command line tool's target. This will make the command line tool have all the files it needs to work, and prevent any bundle/app nesting. This is closer to my original ideal result.

Zend command line tool action error

I solved the problem.

It was coincidence, what led to this error. At first I was unable to even run the tool because I couldn't set include_path (because of permissions). After that there was a problem with (most likely) out-of-date php. (original question is here)

After few hours of headache I deleted the tool and left ZF for a while. Two days ago I wanted to try that on updated server, but coulnd't find the tool so I downloaded it again. Unfortunatelly (for me) Zend released new version of framework and I extracted only the tool.

Briefly

There was a mismatch in versions of the tool (bin) and the framework (library) or possibly the library folder was corrupted.

Solution

Download and extract the whole ZF again to make sure the versions match and files are not corrupted. Also double check the include_path with php -i | grep include_path



Related Topics



Leave a reply



Submit