Skip to content

CxxStdlib link error with C++ interop and explicit module build on Windows #85876

@hjyamauchi

Description

@hjyamauchi

Description

When C++ interop is enabled, which usually would cause the program to linked with the swiftCxxStdlib module, but if it's not actually used (dependency scanning didn't find it used) in a explicit module build on Windows, linking fails with

lld-link: error: could not open 'swiftCxxStdlib.lib': no such file or directory

The swiftCxxStdlib module must be statically linked currently for Windows and the Windows SDK provides the static version libswiftCxxStdlib.lib only. Hence the linker trying to link with the dynamic version swiftCxxStdlib.lib fails.

This is because in the following code the Context.getModuleByName("CxxStdlib") call won't find the module if dependency scanning didn't find it actually used (hasStaticCxxStdlib == false) and registerCxxInteropLibraries will emit the dynamic version of the library file name.

  if (Context.LangOpts.EnableCXXInterop) {
    bool hasStaticCxx = false;
    bool hasStaticCxxStdlib = false;
    if (const auto *M = Context.getModuleByName(CXX_MODULE_NAME))
      hasStaticCxx = M->isStaticLibrary();
    if (Context.LangOpts.Target.getOS() == llvm::Triple::Win32)
      if (const auto *M = Context.getModuleByName("CxxStdlib"))
        hasStaticCxxStdlib = M->isStaticLibrary();
    dependencies::registerCxxInteropLibraries(Context.LangOpts.Target,
                                              getSwiftModule()->getName().str(),
                                              hasStaticCxx,
                                              hasStaticCxxStdlib,
                                              Context.LangOpts.CXXStdlib,
                                              registerLinkLibrary);
  }

Reproduction

Input nocpp.swift

// Anything that doesn't use CxxStdlib

Command

swiftc nocpp.swift -cxx-interoperability-mode=default -explicit-module-build
...
lld-link: error: could not open 'swiftCxxStdlib.lib': no such file or directory

The case where the std clang module is used (CRT depends on it) but not the Swift CxxStdlib module.

Input crt.swift

import CRT

Command

swiftc crt.swift -cxx-interoperability-mode=default -explicit-module-build
...
lld-link: error: could not open 'swiftCxxStdlib.lib': no such file or directory

Expected behavior

Successful build/link

Environment

The main branch on Windows

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions