Skip to content

[SR-2205] Linker fails when linking to Foundation if -static-stdlib flag is present #730

@swift-ci

Description

@swift-ci
Previous ID SR-2205
Radar rdar://problem/34535228
Original Reporter mominul (JIRA User)
Type Bug

Attachment: Download

Environment

DEVELOPMENT-SNAPSHOT-2016-07-25
Ubuntu 16.04

Additional Detail from JIRA
Votes 15
Component/s libdispatch
Labels Bug, Linux
Assignee None
Priority Medium

md5: 76b3df295dd2c0f2cbc48f0a20b3a982

is duplicated by:

  • SR-6434 libdispatch.a missing on Linux

relates to:

  • SR-10625 tests for -static-*
  • SR-9289 libFoundation.a is missing from swift development snapshots

Issue Description:

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

import Glibc
//import Foundation

//var str: NSString = "Hello"
//print(str)

print("Hello, world!")
exit(1)

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions