macOS Command Line Tool with Swift Cocoa Framework: Library not loaded

The above worked for me. the two user build flags and then setting the following in runtime search paths.

LD_RUNPATH_SEARCH_PATHS = $(TOOLCHAIN_DIR)/usr/lib/swift/macosx @executable_path

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

In your "Command Line Tool" Target.. set the following...

LD_RUNPATH_SEARCH_PATHS = @executable_path

(Or as Xcode calls them, "Runtime Search Paths") Setting this, the @rpath, in essence, says.. "hey, look in whatever folder the 'tool' is running in" for the dynamically loadable libs. (In this case it will be your build folder.

Other useful @rpath combinations involve similarly relative paths to the executable (which may also be a library or framework binary, in some cases), via @loader_path. Say if the lib was bundled within a framework.. It may require the LD_RUNPATH_SEARCH_PATHS of @loader_path/Frameworks, etc.