Embedding swiftsyntax in the executable

My implementation that uses swiftsyntax works on my machine. When I distributed the executable, the libSwiftSyntax.dylib is referenced from a local path and fails for others. dyld: Library not loaded:

I am using swift build -c release -Xswiftc -static-stdlib to build the executable and the Package.swift is equivalent to here. Any thoughts on how this problem can be addressed?


The current release of SwiftSyntax is a dynamic library.

Use or modify this Gist. The important parts are that dynamic libraries must be installed alongside the executable, and (on macOS) the linker paths must be switched to relative.

I’m also not sure if -static-stdlib affects the core libraries like Foundation, which SwiftSyntax links against. So I kind of recommend installing from source on each machine to guarantee that the location of the core libraries is correct (as opposed to distributing a built executable). The Gist is designed to be directly executed to accomplish just that.

Here is an example of a project that uses the Gist to install. It even depends on SwiftSyntax, albeit indirectly as a sub‐dependency.

Thanks for the suggestion. I also found another workaround:

Do a install_name_tool -change <local-path-to-libSwiftSyntax.dylib> @executable_path/libSwiftSyntax.dylib tool-executable and then distribute the tool-executable along with libSwiftSyntax.dylib. This seems to work.

Any possible objections to this approach?

That was exactly what I meant by “or modify”, since that is a reduced version of what the Gist does.

The note above about -static-stdlib and Foundation still stands though. You may run into issues if Swift itself is installed somewhere else on the target device compared to where it was installed on the device where it was built.

1 Like


I thought it was listed there, but I cannot seem to find it on bugs.swift.org anymore. Do you know if the absolute path issue has been fixed (or is fixable) since 4.2.1?

Also, what do you think of adding a command which would build and then copy all the necessary product components someplace without the intermediate build artifacts. Something along the lines of this:

swift build-for-install --output ../Installables/

If the original poster were to use it, he (or she) would end up with the executable and the dynamic library being in the Installables directory, but nothing else.