Uploaded image for project: 'Swift'
  1. Swift
  2. SR-2205

Linker fails when linking to Foundation if -static-stdlib flag is present

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: libdispatch
    • Labels:
    • Environment:

      DEVELOPMENT-SNAPSHOT-2016-07-25
      Ubuntu 16.04

      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!

        Attachments

        1. errors.txt
          127 kB
        2. test.swift
          0.1 kB

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                mominul Muhammad Mominul Huque
              • Votes:
                13 Vote for this issue
                Watchers:
                30 Start watching this issue

                Dates

                • Created:
                  Updated: