Skip to content

undefined reference to 'sqrt' unless the math library is explicitly linked #8773

Open
@tayloraswift

Description

@tayloraswift

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

  • Confirmed reproduction steps with SwiftPM CLI. The description text must include reproduction steps with either of command-line SwiftPM commands, swift build, swift test, swift package etc.

Description

When compiling a Swift package containing basic math operations like squareRoot(), the linker fails with undefined reference to 'sqrt' unless the math library is explicitly linked.

Steps to Reproduce

  1. Create a minimal Swift package:
mkdir MathLinkTest && cd MathLinkTest
swift package init --type executable
print(1.squareRoot())
$ swift run
warning: /home/ubuntu/.swiftpm/configuration is not accessible or not writable, disabling user-level cache features.
warning: /home/ubuntu/.swiftpm/security is not accessible or not writable, disabling user-level cache features.
Building for debugging...
error: link command failed with exit code 1 (use -v to see invocation)
/home/ubuntu/.local/share/swiftly/toolchains/6.1.0/usr/lib/swift/shims/LibcShims.h:158: error: undefined reference to 'sqrt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[7/8] Linking SwiftMathLinkTest

$ swift --version
Swift version 6.1 (swift-6.1-RELEASE)
Target: x86_64-unknown-linux-gnu

i am using the SwiftPM that shipped with the toolchain.

the workaround i am using is

.executableTarget(
    name: "MathLinkTest",
    dependencies: [],
    linkerSettings: [
        .linkedLibrary("m")
    ]
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions