Linking error: cannot find lDispatchStubs and lCoreFoundation when using -static-stdlib

Hello,
I have a project with hummingbird as a dependency that I would like to build into a statically linked excecutable.

When I build a release with the "-static-stdlib" flag I get this error:

Building for production...
error: link command failed with exit code 1 (use -v to see invocation)
/usr/bin/ld.gold: error: cannot find -lDispatchStubs
/usr/bin/ld.gold: error: cannot find -lCoreFoundation
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
error: fatalError

I use Ubuntu (WSL2). I assume that I'm missing some dependency.
But which one and how can I install it?

So far I'm unable to resolve this problem using google.

These should be automatically supplied by the toolchain, so something is going wrong. You'll need to provide the full build command you are running and the full error output from running in verbose mode by passing -v to SwiftPM.

Sure, here is the command, Package.swift, and error log.

Command: swift build --configuration release

Package.swift:

import PackageDescription

let package = Package(
    name: "nomnomnom",
    dependencies: [
        .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "0.15.2"),
        .package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.3"),
    ],
    targets: [
        .target(
            name: "nomnomnom",
            dependencies: [
                .product(name: "ArgumentParser", package: "swift-argument-parser"),
                .product(name: "Hummingbird", package: "hummingbird"),
                .product(name: "HummingbirdFoundation", package: "hummingbird")
            ],
            swiftSettings: [
                .unsafeFlags(["-Osize", "-whole-module-optimization", "-static-stdlib"])
            ]
        )
    ]
)

the error log is quite long. I uploaded it as a gist: Swift build static release error ยท GitHub

Edit: The Definition of the Package.swift might not be optimal, or contain errors as I don't yet really understand the package manager.
However, I get the same error when I add the "-static-stdlib" Flag to the Package.swift definition of the official humminbirg example: https://github.com/hummingbird-project/hummingbird-examples/tree/main/hello

OK, I'm able to reproduce with your linked hello example. The problem appears to be that -static-stdlib is both a compilation and linker flag, which SwiftPM separates out for maximum configurability. You have two choices to build and link against the static stdlib:

  1. If you only want that single SwiftPM executable target to be linked against the static stdlib, also add a linkerSettings: [.unsafeFlags(["-static-stdlib"])] right after the swiftSettings you added.
  2. If you don't mind all targets in the package to be built and linked against the static stdlib, add the --static-swift-stdlib flag on the command-line instead. This adds -static-stdlib both to all Swift compilation arguments and linker arguments for executable targets, so everything in the package builds against the static stdlib.

I just tried both approaches for that hello Server executable, and checked that both worked:

> readelf -d .build/x86_64-unknown-linux-gnu/release/Server | ag NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libutil.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]

Thank you so much :smiley:
Both solutions work for my project.