Compile Swift for Raspberry Pi by Visual Studio Code

Programming passion
4 min readDec 27, 2018

--

In order to keep this tutorial short, I assume that you have had Raspbian OS, Swift and Swish in place, otherwise please check out these following posts:
* Set up Raspbian Strech Lite OS
* Set up Swift 4.1 environment
* Setup Swish and compile Swift for Raspberry Pi by XCode

Visual Studio Code is free, and available on Window, Mac and Linux. But this tutorial limits for the guys who use Mac or Linux. Because I’m not sure if there are rsync, ssh on Window or not. If available, not sure they function as same as on Mac and Linux.

The mechanism which compiles swift sources for Raspberry Pi on Visual Studio Code is as same as XCode. Visual Studio Code will sync up source code to Raspberry Pi, then send compilation command over ssh to have them compiled on Raspberry Pi.

The benefit here is you take advantage of Visual Studio Code instead of using a boring command-line editor.

Create project

Ok, let get started by create a new project named “Raspberry_Pi_In_Swift” by these following commands:

Create a new folder

mkdir Raspberry_Pi_In_Swift

Jump in the folder

cd Raspberry_Pi_In_Swift

Create a new project

swift package init --type executable

After all, open the project in Visual Studio Code in menu File > Open or Cmd + O and choose the Raspberry_Pi_In_Swift folder.

You will see the Swift project opened like the following picture.

Config build task

In visual studio code, open menu item Terminal > Configure Tasks, then select “ Create tasks.json from template” and choose “ Others — example to run an arbitrary external command

Configure Build Task — Step 1
Configure Build Task — Step 2
Configure Build Task — Step 3

Now the task is created, set a label for it. It can be anything but choose an appropriate label, for example “Remotely build on Raspberry Pi”.

In the command, put swish command in there with the raspberry pi’s user and ip address. Then save and close it.

In the menu, choose Terminal > Configure Default Build Task, and choose the task which you have created earlier.

Before building, make sure in Package.swift, swift-tools-version version must be appropriate to the swift on the Raspberry Pi. It should be 4.1 for Swift 4.1.x

Here We go. Choose Terminal > Run Build Task or Shift + Cmd + B to build the project. You will see the following console output.

On the Raspberry Pi end, the project is located at home/pi/swish/Raspberry_Pi_In_Swift

Jump in the project folder by

cd home/pi/swish/Raspberry_Pi_In_Swift

and run the compiled program

swift run --skip-build

What’s next

Visual Studio Code is free, simple and lightweight editor. It’s literally friendly to beginner. However, in term of features, functionalities, XCode is outweighed Visual Studio Code. (Learn more here -> Compile Swift for Raspberry Pi by XCode )

The XCode has a decent IntelliSense for Swift. It also supports running and debugging Swift.

In order take advantages of XCode, I will share you some useful strategies to help write code for Raspberry Pi which you can run and debug straight on MAC/XCode, so stay tuned

Originally published at http://swiftreviewercom.wordpress.com on December 27, 2018.

--

--

Programming passion

Been working on iOS, tvOS, Swift, and Objective-C since once upon a time. I'm just a little iOS developer, but like to mess with everything.