I come across of this situation when I was trying to compile my code as statically linked with stdlib.
If I compile my code without importing Foundation with -static-stdlib flag, the compilation succeeds.
But the actual error occurs when I try to import Foundation. As the linker fails when linking with Foundation.
/usr/bin/ld.gold: error: cannot find -lFoundation
/tmp/main-0993c4.o:/tmp/main-0993c4.o:function main: error: undefined reference to '_TMaC10Foundation8NSString'
/tmp/main-0993c4.o:/tmp/main-0993c4.o:function main: error: undefined reference to 'TFC10Foundation8NSStringCfT13stringLiteralVs12StaticString_S0'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
As I tried to inspect the error with my little knowledge, here is the fact that took my attention:
- Without the `-static-stdlib` flag the library search path is set as /path/to/swift/usr/lib/swift/linux where the shared libraries are stored including Foundation.
- But with the `-static-stdlib` flag the library search path is set as /path/to/swift/usr/lib/swift_static/linux where the static libraries are stored excluding Foundation.
So linker is innocent!
Can it be solved by adding statically linked copy of Foundation?
So is there any solutions? Can I compile my code statically linked with stdlib and Foundation?
To reproduce it, is very easy