Can not compile & ldquo; hello world & rdquo; program with Swift on Ubuntu 14.04


Using Ubuntu 14.04

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:    14.04
Codename:   trusty

and trying to compile a hello world Swift program.

print("Hello World!")

I have verified the download:

 gpg --verify swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu14.04.tar.gz.sig
gpg: Signature made Thu 10 Dec 2015 07:17:37 PM PST using RSA key ID 412B37AD
gpg: Good signature from "Swift Automatic Signing Key #1 <[email protected]>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7463 A81A 4B2E EA1B 551F  FBCF D441 C977 412B 37AD

But when I try and compile the hello world program I get this error:

> swiftc hello_world.swift
<unknown>:0: error: opening import file for module 'SwiftShims': No such file or directory

If I start up the Swift REPL I get a similar error:

~$ swift
Welcome to Swift version 2.2-dev (LLVM 7bae82deaa, Clang 53d04af5ce, Swift 5995ef2acd). Type :help for assistance.
  1> y = 6
opening import file for module 'SwiftShims': No such file or directory

If I follow the instructions given here on using "swift build" versus "swiftc" for compiling a Hello World Swift program, I get a different error:

$ swift build
<unknown>:0: error: opening import file for module 'Swift': No such file or directory
swift-build: exit(1): ["/home/scooter/Programs/Swift/V2.2/bin/swiftc", "--driver-mode=swift", "-I", "/home/scooter/Programs/Swift/V2.2/lib/swift/pm", "-L", "/home/scooter/Programs/Swift/V2.2/lib/swift/pm", "-lPackageDescription", "/home/scooter/code/swift/helloworld-project/Package.swift"]

Executing the interpreter:

$ swift hello_world.swift


<unknown>:0: error: opening import file for module 'SwiftShims': No such file or directory

strace gets an error trying to run swift:

$ strace  -o /tmp/swift.log -- swift <(echo '1 + 1')
<unknown>:0: error: opening import file for module 'SwiftShims': No such file or directory

open("/home/user/Programs/Swift/V2.2/lib/swift/linux/x86_64/SwiftShims.swiftmodule", O_RDONLY) = -1 ENOENT (No such file or directory)

Sorry for adding as an answer what should have really been a comment content-wise, but it is a little too long for a comment.

As far as I can tell, this is indeed the same problem as described in Swift on Linux: Make very first step work. I have been looking into this myself in my spare time, but no luck so far. The user who asked the other question has been pursuing it and has some interesting recent updates there.

A few things to try:

See if the swift interpreter works. Just type swift hello_world.swift and see what happens. I don't think it will work. If it does not, then run the strace command as follows:

strace -o /tmp/swift.log -- swift <(echo '1 + 1')

and look at /tmp/swift.log. See what files cannot be found, especially near the end of the output. Warning: even on a system where the error doesn't happen (I haven't been able to reproduce it yet), the strace output shows a lot of No such file... errors.

You can also try swift and swiftrc with the -v option to enable verbose output and see if you notice anything suspicious.

Update 1/2/2016:

The question referenced earlier has been updated with a possible solution to the problem: get rid of non-standard installations of gcc, g++, libgcc, and libstdc++. Please see the comments in the other question.

Update 1/3/2016:

Using the clues from the discussion on the other question, I've been able to reproduce the problem by installing gcc-5.1.0 from source and pre-pending the location of the newly-installed to the LD_LIBRARY_PATH variable.

The problem could be solved as follows: 1) Figure out where from the older package is installed. On my system:

user@ubuntu14:~$ dpkg -l | grep libstdc++
ii  libstdc++-4.8-dev:amd64            4.8.4-2ubuntu1~14.04             amd64        GNU Standard C++ Library v3 (development files)
ii  libstdc++6:amd64                   4.8.4-2ubuntu1~14.04             amd64        GNU Standard C++ Library v3

user@ubuntu14:~$ dpkg -L libstdc++6 | grep

2) Prepend the location of to $LD_LIBRARY_PATH:

export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH

3) Double-check that the swift binary is using the correct shared library:

user@ubuntu14:~$ ldd `which swift` | grep libstdc++ => /usr/lib/x86_64-linux-gnu/ (0x00007fdd1476b000)

Now Swift should work without the error in question.