[SOLVED] Testing my changes: ld: library not found for -lobjc

Beginners question:

I'm trying to test some changes to the stdlib and compiler for the upcoming pitch. I'm happy with generated SIL and IR, and now I want to compile and run a complete executable.

If I run without specifying SDK, I'm getting a linker error:

../build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/bin/swiftc -parse-as-library ~/foo.swift -o ~/foo -v
Swift version 5.8-dev (LLVM 85bf33908e790ef, Swift 49ab8fe05889d16)
Target: arm64-apple-macosx12.0
/Users/npohilets/git_tree/swift-project/build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/bin/swift-frontend -frontend -c -primary-file /Users/npohilets/foo.swift -target arm64-apple-macosx12.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -color-diagnostics -new-driver-path /Users/npohilets/git_tree/swift-project/build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/bin/swift-driver -empty-abi-descriptor -resource-dir /Users/npohilets/git_tree/swift-project/build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/lib/swift -module-name foo -parse-as-library -o /var/folders/f6/qllm_pk56m56tdvyx0g_4d3m0000gp/T/TemporaryDirectory.eOx5zj/foo-1.o
/Applications/Xcode-13.3.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld /var/folders/f6/qllm_pk56m56tdvyx0g_4d3m0000gp/T/TemporaryDirectory.eOx5zj/foo-1.o /Users/npohilets/git_tree/swift-project/build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/lib/swift/clang/lib/darwin/libclang_rt.osx.a -lobjc -lSystem -arch arm64 -L /Users/npohilets/git_tree/swift-project/build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/lib/swift/macosx -platform_version macos 12.0.0 0.0.0 -o /Users/npohilets/foo
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lobjc

If I specify SDK in the compiler invocation, it picks up standard library from the OS, and does not find new runtime function.

How can I produce a binary that links with custom stdlib for testing purposes?

If you've developed your own custom version of libobjc.dylib, you probably need to add a -L <path/to/libobjc.dylib>. Otherwise, the system library should suffice (-sdk specification points to the appropriate version for the platform in use, which is why it worked).

Looks like I misinterpreted symptoms. It was linking with my stdlib, but I didn't export my runtime function properly - had to add an entry to CompatibilityOverridesConcurrency.def to fix it.

Now linked executable links successfully, but crashes at runtime, because framework paths in the executable still point to the system stdlib:

$ ../build/Ninja+cmark-DebugAssert+llvm-RelWithDebInfoAssert+swift-DebugAssert+stdlib-DebugAssert/swift-macosx-arm64/bin/swiftc -parse-as-library ~/foo.swift -o ~/foo -L /Applications/Xcode-13.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/
$ otool -L ~/foo
/Users/npohilets/foo:
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
	/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
	/usr/lib/swift/libswift_Concurrency.dylib (compatibility version 1.0.0, current version 0.0.0)

Hm.. setting DYLD_FRAMEWORK_PATH didn't work for some reason, but loading specific library using DYLD_INSERT_LIBRARIES did work. But for now, that's good enough.

libswiftCore is not a framework, so that makes sense. There’s also DYLD_LIBRARY_PATH.

1 Like