Uploaded image for project: 'Swift'
  1. Swift
  2. SR-14728

SPM/Driver on Windows fails to link C-based targets

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Package Manager
    • Environment:

      OS: Windows 10 Pro (20H2)

      Toolchain: compnerd.org Swift version 5.5-dev (LLVM 76b16fe31111aa0, Swift 5eb25f1662051fd)

      Description

      When using SPM on Windows to build a `.dynamic` library which only contains C targets, the linker fails with the following error: 

      lld-link: error: undefined symbol: __declspec(dllimport) swift_addNewDSOImage
      >>> referenced by C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86_64\swiftrt.obj:(void 
      __cdecl swift_image_constructor(void))
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      

      Running `swift build -Xswiftc -v` reveals what seems to be the issue:

      "C:\\Library\\Developer\\Toolchains\\unknown-Asserts-development.xctoolchain\\usr\\bin\\lld-link" "-out:C:\\Users\\kabir\\Desktop\\Code\\WinBug\\.build\\x86_64-unknown-windows-msvc\\debug\\WinBug.dll" -dll <...> "C:\\Library\\Developer\\Platforms\\Windows.platform\\Developer\\SDKs\\Windows.sdk\\usr\\lib\\swift\\windows\\x86_64\\swiftrt.obj" "C:\\Users\\kabir\\Desktop\\Code\\WinBug\\.build\\x86_64-unknown-windows-msvc\\debug\\WinBug.build\\WinBug.c.o"
      

      i.e. the Swift driver appears to be linking swiftrt.obj but not swiftCore.lib (since swift-autolink-extract sees no need for swiftCore.lib because it's a pure C target). Using the driver by itself (instead of through swift build) also produces this issue.

      I'm not too familiar with the Swift codebase but an initial search seems to indicate that the issue is caused due to swiftrt.obj being unconditionally linked at lib/Driver/WindowsToolChains.cpp#L134. Running `swift build -Xswiftc -lswiftCore` causes it to link successfully – though I suspect that not linking swiftrt.obj in C-only targets might be the preferred solution.

      I've attached a minimal project demonstrating the issue; running `swift build` succeeds on both macOS and Linux, but fails on Windows.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            kabiroberai Kabir Oberai
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: