Linux static-executable linking errors

There are two issues here:

  1. You seem to have a dependency on FoundationNetworking which has a transitive dependency on libcurl. For this to work you also have to statically link in libcurl.
  2. You also have a dependency on libc. When using -static-executable swift will statically link libc which is not really supported and that's why you get these warnings: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking. Which kind of defeats the purpose.

Regarding 1. you can get this to work by checking this post on how to find the transitive dependencies and statically link against them during build.

Regarding 2. In the same thread you can also find a lengthy discussion about the quirks related to -static-executable and libc.

My recommendation would be to NOT use -static-executable but instead use -static-swift-stdlib. The latter will statically link the Swift runtime libraries into your binary, so you can deploy it to a system without Swift installed. You would still need the runtime dependencies (such as libcurl, etc) available for dynamic linking.

In Docker world sth like this works well (see Docker in swift-server/guides)

#------- build -------
FROM swift:centos8 as builder

# set up the workspace
RUN mkdir /workspace
WORKDIR /workspace

# copy the source to the docker image
COPY . /workspace

RUN swift build -c release --static-swift-stdlib

#------- package -------
FROM centos
# copy executables
COPY --from=builder /workspace/.build/release/<executable-name> /

# set the entry point (application name)
CMD ["<executable-name>"]
3 Likes