Swift on Linux: Make very first step work

2019-04-05 00:04发布

I am totally new to swift. It has just been released as open source for linux and I wanted to try it. This is on ubuntu 14.04. clang is installed as per prerequisites.

<Edit>: requirements here request clang version 3.6 also on ubuntu 14.04. I had first tried these first steps with clang 3.4, but have since updated to 3.6 following the instructions in the link and retried. Same result.</Edit>

I have downloaded https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz to ~/Downloads/. Transcript of installation and first step in swift:

$ cd /tmp
$ tar xf ~/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz 
$ PATH=/tmp/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin/:"$PATH"
$ clang++ --version
Ubuntu clang version 3.6.0-2ubuntu1~trusty1 (tags/RELEASE_360/final) (based on LLVM 3.6.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ which swift
/tmp/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin//swift
$ swift --version
Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Target: x86_64-unknown-linux-gnu
$ swift
Welcome to Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c). Type :help for assistance.
  1> 1 + 2
opening import file for module 'SwiftShims': No such file or directory
  1>  

According to this getting started guide it should have printed instead

$R0: Int = 3

What's wrong and how can I fix it?

Edit: Trying to find that file manually: It is apparently not contained in the installation:

$ find swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/ | grep -i shims
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/FoundationShims.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/SwiftStddef.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/module.map
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/UnicodeShims.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/GlobalObjects.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/HeapObject.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/RuntimeShims.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/RefCount.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/RuntimeStubs.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/LibcShims.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/CoreFoundationShims.h
swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/shims/SwiftStdint.h

Edit: When I try the swift build helloworld example, this output is produced:

/tmp/Hello$ swift build
<unknown>:0: error: opening import file for module 'Swift': No such file or directory
swift-build: exit(1): ["/tmp/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin/swiftc", "--driver-mode=swift", "-I", "/tmp/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/pm", "-L", "/tmp/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/pm", "-lPackageDescription", "/tmp/Hello/Package.swift"]

Edit: new findings up to 2015-12-22

Since I posted this question, at least two more snapshots of swift for linux have been released: 2015-12-10 and 2015-12-18. I have tried these, but they do not fix the issue.

This mailing list thread here (post 1, post 2, post 3) is about the same problem. It was suggested there that installation of swift under the home directory would help. It did not help in my case, the error message is still the same.

More interestingly, that mailing list thread contains an analysis of the REPL error created with strace. It would be interesting to compare that particular strace output with the strace output from a system where REPL works.

Edit: More info up to 2015-12-26:

A new swift release is out, dated 2015-12-22. This release exhibits the same problem.

Another thread concerning this problem has appeared on the swift mailing list starting here. The user experiencing the problem has also posted strace output here.

5条回答
手持菜刀,她持情操
2楼-- · 2019-04-05 00:20

This is not really an answer -- I have the same problem as OP -- but SwiftShims is actually defined the module.map file in your file listing above:

module SwiftShims {
  header "CoreFoundationShims.h"
  header "FoundationShims.h"
  header "GlobalObjects.h"
  header "HeapObject.h"
  header "LibcShims.h"
  header "RefCount.h"
  header "RuntimeShims.h"
  header "RuntimeStubs.h"
  header "SwiftStddef.h"
  header "SwiftStdint.h"
  header "UnicodeShims.h"
  export *
}

https://github.com/apple/swift/blob/8d9ef80304d7b36e13619ea50e6e76f3ec9221ba/stdlib/public/SwiftShims/module.map

查看更多
仙女界的扛把子
3楼-- · 2019-04-05 00:25

I had the exact same problem. It turns out that I had added the ppa:ubuntu-toolchain-r/test repo in order to install g++-4.9 on my Mint distro (17.2). Once I purged the repository and restored various libraries to their original versions, swift finally worked for me.

Specifically, I had to run

sudo apt-get install ppa-purge
sudo ppa-purge -d trusty ppa:ubuntu-toolchain-r/test

While cleaning up, ppa-purge was complaining that in order to resolve conflicts, it would have to remove quite a few packages it could not find in the Ubuntu Trusty repo (including really core ones like build-essential, xorg, gcc, x11-xserver-utils...), so I made a note and reinstalled these right away after the purge. Just be very careful.

I think some of the libraries overridden when installing g++ 4.9 were creating a conflict. I've verified all this on a fresh Mint install too.

查看更多
别忘想泡老子
4楼-- · 2019-04-05 00:28

Following the advice of James D, I tried to run

sudo apt-get install ppa-purge
sudo ppa-purge -d trusty ppa:ubuntu-toolchain-r/test

However, this did not work. What's strange, is that what did work doesn't make sense. For me, I got it working by installing the above ppa first and then purging it. The whole command set that got me working was

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9 # This step may be optional

sudo apt-get install ppa-purge
sudo ppa-purge -d trusty ppa:ubuntu-toolchain-r/test
查看更多
叼着烟拽天下
5楼-- · 2019-04-05 00:32

I repeated the same steps as you described on a brand-new Ubuntu 14.04 Azure VM and got the expected

$R0: Int = 3

Interestingly, it worked fine even without Clang or GCC! Later I installed Clang-3.5, and it worked as well.

Is it possible that they have sneaked in a new tarball with the same name? The MD5 hash of the one I got is here:

user@ubuntu1:/tmp/junk$ md5sum swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz 
a93f52921c491b747cad256904c8742f  swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz

Does yours match? If so, you may want to try a different installation of Ubuntu 14.04 if you have access to one.

I have also been able to successfully use swift build as instructed in https://swift.org/getting-started/#using-the-build-system . Removing the clang-3.5 package broke swift build, just as I had suspected, but REPL swift still worked as it did originally, before installing Clang for the first time. Then I installed Clang-3.4, and swift build was back in business.

Update 1/3/2016:

Using the hints from the various comments on this question, I've been able to reproduce the error on my Ubuntu 14.04 Azure VM. As an alternative solution, the problem can also be addressed by manipulating $LD_LIBRARY_PATH, see Unable to compile "hello world" program with Swift on Ubuntu 14.04.

查看更多
闹够了就滚
6楼-- · 2019-04-05 00:33

Had the same issue, what I missed was that I didn't get the keys for the package and verify it before extracting.

  1. Download the package
  2. wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import
  3. gpg --keyserver hkp://pool.sks-keyservers.net --refresh-keys Swift
  4. gpg --verify swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz.sig
  5. tar xzf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz
  6. add PATH=/path/to/usr/bin:"${PATH}" to your .bashrc
  7. finally run swift

You should see:

Welcome to Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c). Type :help for assistance.
1> 1+1
$R0: Int = 2
查看更多
登录 后发表回答