LoginSignup
13
5

More than 3 years have passed since last update.

Raspberry Pi で Swift を使えるようにする【Swift 5】

Last updated at Posted at 2020-07-27

Raspberry Pi に Swift をインストールし、「Hello, world!」を出力するまでをやってみます。

環境

Mac にインストールした Visual Studio Code で Raspberry Pi 3 Model B にSSH 接続。

Mac 側の環境

  • MacBook Pro (Retina, 15-inch, Late 2013)
  • macOS Catalina バージョン 10.15.6(19G73)
  • Visual Studio Code バージョン: 1.47.2
    • コミット: 17299e413d5590b14ab0340ea477cdd86ff13daf
    • 日付: 2020-07-15T18:18:50.054Z
    • Electron: 7.3.2
    • Chrome: 78.0.3904.130
    • Node.js: 12.8.1
    • V8: 7.8.279.23-electron.0
    • OS: Darwin x64 19.6.0

Raspberry Pi 側の環境

  • Raspberry Pi 3 Model B
$ cat /etc/issue
Raspbian GNU/Linux 9 \n \l

$ cat /etc/debian_version
9.11

$ uname -a
Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux

$ cat /proc/version
Linux version 4.19.66-v7+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1253 SMP Thu Aug 15 11:49:46 BST 2019

$ bash --version
GNU bash, バージョン 4.4.12(1)-release (arm-unknown-linux-gnueabihf)

Swift-Arm の入手

Swift-Arm を入手します。以下のスクリプトで OS の判別、curl のチェック、gpg のチェック等が行われ、Swift-Arm がインストールされます。

$ curl -s https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh | sudo bash

Swift のインストール

$ sudo apt-get install swift5

必要なパッケージも一緒にインストールされます。

インストールした Swift のバージョンを確認

$ swift --version
Swift version 5.1.5 (swift-5.1.5-RELEASE)
Target: armv6-unknown-linux-gnueabihf

無事に Swift をインストールできました🎉

ディレクトリと初期ファイルの作成

今回は SwiftPractice というディレクトリを作成します。swift package で初期ファイルたちを作成します。

$ mkdir SwiftPractice
$ cd SwiftPractice
$ swift package init --type executable

sc001.png

生成された main.swift には print("Hello, world!") と書かれています。

ビルドと実行

ビルドして実行します。

$ swift build
[4/4] Linking SwiftPractice
$ swift run
Hello, world!

ビルドに成功し、正しく「Hello, world!」が出力されました🎉🎉🎉

参考資料

13
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
5