"Could not find or use auto-linked library" during linking

Hey! I would greatly appreciate your help with the following problem:

I'm trying to link multiple .o files to create an executable using the Swift toolchain (not using Xcode or SwiftPM). Previously (using the swift-5.2.4-RELEASE toolchain), the following command did the job and emitted an executable:

/path/to/toolchain/usr/bin/swift --driver-mode=swiftc -emit-executable main.o library.o anotherLibrary.o

But when I use the swift-5.3-RELEASE toolchain, I get these errors:

ld: warning: Could not find or use auto-linked library 'swiftQuartzCore'
ld: warning: Could not find or use auto-linked library 'swiftsimd'
ld: warning: Could not find or use auto-linked library 'swiftGLKit'
ld: warning: Could not find or use auto-linked library 'swiftSceneKit'
ld: warning: Could not find or use auto-linked library 'swiftModelIO'
ld: warning: Could not find or use auto-linked library 'swiftMetal'
ld: warning: Could not find or use auto-linked library 'swiftCoreImage'
ld: warning: Could not find or use auto-linked library 'swiftAppKit'
ld: warning: Could not find or use auto-linked library 'swiftIOKit'
ld: warning: Could not find or use auto-linked library 'swiftXPC'
ld: warning: Could not find or use auto-linked library 'swiftCoreData'

When I compared the package content of the two versions of the toolchain, I noticed that the 5.2.4 toolchain included all of the libraries that I need in the usr/lib/swift/macosx directory, while the 5.3 toolchain doesn't seem to include these.
I'm not sure if this is the cause of the problem, but if it is, I suppose I need to give the compiler a search path for these libraries on my machine? What would that look like? Or am I missing something else?

Again, any ideas or help would be greatly appreciated!

Found the issue!
I needed to set the path to the macOS SDK. The command now looks like this:

/path/to/toolchain/usr/bin/swift --driver-mode=swiftc -emit-executable main.o library.o anotherLibrary.o -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk

I get the same issue but my repro is totally different, and I'm building with Xcode. As soon as I set my project to a Deployment Target of 14.0, the error appears, and then when I revert that change, it disappears. Couldn't find anything in the Xcode 12 release notes that referenced this.

ld: warning: Could not find or use auto-linked library 'swiftAVFoundation'
Undefined symbols for architecture x86_64:
  "_swiftoverride_class_getSuperclass(swift::TargetMetadata<swift::InProcess> const*)", referenced from:
      swift::swift50override_conformsToProtocol(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolDescriptor<swift::InProcess> const*, swift::TargetWitnessTable<swift::InProcess> const* (*)(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolDescriptor<swift::InProcess> const*)) in libswiftCompatibility50.a(ProtocolConformance.cpp.o)
  "swift::swift51override_conformsToSwiftProtocol(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolDescriptor<swift::InProcess> const*, llvm::StringRef, swift::TargetProtocolConformanceDescriptor<swift::InProcess> const* (*)(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolDescriptor<swift::InProcess> const*, llvm::StringRef))", referenced from:
      _Swift50Overrides in libswiftCompatibility50.a(Overrides.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Happy to share project with anyone at Apple if they want to debug.

Could you file a bug report through feedbackassistant.apple.com? That should allow you to attach your project and provide reproduction steps.

Did anyone find clear fixes to these? I'm hitting this same case in a project where there is definitely some messing with auto-linking, but I'm not sure what library to provide to satisfy this error when deploying for iOS 11

In my case passing -lswiftCompatibility51 to the link resolves the issue. So I guess something is wrong earlier in the chain (likely in the tooling that produced this binary) that causes that not to be present in the .o files LC_LINKER_OPTION's

1 Like

Hi -- this warning is appearing for me too in an Xcode Project:

'Could not find or use auto-linked library' subsequently with a bunch of errors stemming from 'Undefined symbols for architecture arm64'. Having read through this topic, it seems similar, although the library files in this case were placed directly into "CMyLibrary". Any pointers in this scenario would be greatly appreciated.

Swift Package that wraps a C Library with the manifest as so:

let package = Package(
    name: "CMyLibrary",
    platforms: [
        .macOS(.v11),
        .iOS(.v15)
    ],
    products: [
        .library(name: "CMyLibrary", targets: ["CMyLibrary"])
    ],
    dependencies: [
        // .package(url: /* package url */, from: "1.0.0"),
    ], targets: [
        .target(name: "CMyLibrary", dependencies: [])
    ]
)

module.map looks like this:

module CMyLibrary {
  header "mylibrary.h"
  link "mylibrary"
  export *
}

Then created a library to interface with the wrapper, with the following manifest:

let package = Package(
    name: "MyLibrary",
    platforms: [
        .macOS(.v11),
        .iOS(.v15)
    ],
    products: [
        .library(
            name: "MyLibrary",
            targets: ["MyLibrary"]),
    ],
    dependencies: [
        // .package(url: /* package url */, from: "1.0.0"),
        .package(path: "../CMyLibrary"),
        .package(path: "../SomeKit"),
    ],
    targets: [
        .target(
            name: "MyLibrary",
            dependencies: ["CMyLibrary","SomeKit"]),
    ]
)

This worked for me:

Sorry for unearthing a old thread now. I had to do some linking between a swift library and a C library and for learning's sake trying to do this with ld instead of swiftc on the command line.

My Makefile looks like this

hello: swift.o hello.o
    @#ld -L{link_dir} {-l...} -o $@ $^ # this doesn't work!
    swiftc -o $@ $^

swift.o: main.swift
    swiftc -emit-object -static -import-objc-header bridge.h -o $@ $^

hello.o: hello.c
    clang -c -o hello.o hello.c

The final swiftc command for linking to create the executable works but what's the corresponding incantation for ld? I tried putting -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib but in that directory there are only *.tbd files and they seem to mention other *.dylib files in /usr/lib/swift but I don't see those files.

Yes, swiftc gets the job done but I'm wondering how I can do this, albeit the roundabout way using ld.