Click here to Skip to main content
15,868,016 members
Articles / Mobile Apps / iOS
Article

Learn Swift Without a Mac: Compile, Build & Run Swift Code on Windows

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
26 Oct 2016CPOL9 min read 53.6K   662   5   13
Want to learn to write Swift code, but you don't have a Mac yet? Now you can write and compile Swift on Windows. Only takes about 15 minutes to set up.

Introduction

Recently, I was quite desperate for a way to write some Swift code without being required to have a Mac available.  Swift is an open source language so I hoped and then I Googled. 

Somehow I stumbled upon an Infoworld article (Swift for Windows arrives at last, but as an unofficial port | InfoWorld[^]) which had a link to GitHub repo that had pre-built binaries that supposedly would help me compile, build and run Swift code.  I figured it was worth a try.  

I Was Shocked It Actually Worked

I followed the cryptic steps at the GitHub link, wrote the most basic Swift test code, compiled, built it and ran it. I couldn't believe that it had actually turned my Swift code into a honest-to-goodness Windows EXE!!!

Prerequisites & Caveats

The information at the GitHub link mentions a few things that are required to build Swift on Windows and I want to mention those and a few other things here so you don't get your hopes up that you are going to be able to build an iOS app on Windows.  This will not allow you to do that.  It will allow you to learn Swift syntax and some Swift API, but you will not be able to write iOS apps.  The end result is a Windows binary that runs your limited Swift code.  

  1. Using the binaries whichi allow you to compile and build your Swift code requires your machine to be running a 64 bit processor
  2. You're going to need the C++ Redistributable for Visual Studio 2015
  3. You must have the Visual Studio 2015 SDK installed.

What Do All of Those Requirements Really Mean?

  1. Be running a 64 bit machine.
  2. Make sure you install Visual Studio 2015 Community Edition

I am running Windows 2012 R2 in a VM and I've installed Visual Studio 2015 Community Edition and I followed the steps (we'll talk about in a moment) and I got this working in about 15 minutes.

The Caveats

What do you really get from this process?

Once you do this, you will be able to compile and build Swift code.  However, you need to know there are some limitations:

  1. The process is able to compile Swift 2.x code.  Does not seem to support Swift 3.x syntax and commands
  2. Not all libraries are available.  Things like UIKit and Foundation libraries are not fully available.
  3. This builds a console application.  This does not build any graphic UI representation.  This is purely so you can learn basic Swift syntax and methods etc.

What are the benefits?

There are numerous benefits but admittedly they are mostly for those who simply want to learn some Swift code but don't want to buy a Mac yet.

  1. Learn Swift syntax
  2. Learn to write pure Swift as you concentrate only on Swift -- As you're learning it's actually nice to only focus on the language itself first.  This will keep the noise of learning Cocoa (iOS UI library) and other items seperate. 
  3. The syntax and skills you learn will directly apply to code you will write when you build your iOS apps.

How Building Swift Code On Windows Was Helpful To Me

Swift (and iOS) development is quite different from Windows development.  I've been developing software on Windows for over 25 years now, but I just started learning iOS dev.  I have a headless Mac Mini that isn't always available to me, but I'm in the middle of developing an iOS app (iPhone, iPad). 

I had a couple of pure methods I was need to write and they were based upon parsing a Swift String and manipulating it.  You will see that indexing through a String in Swift is far different than most libraries on Windows (JavaScript, C#, C++, VisualBasic). 

I Needed A REPL

This code was entirely seperate from what I was doing in the app and I knew if I could just get a playground I could write these pure functions and then add them back into my XCode project.  

I just needed a way to REPL through some pure Swift code and this environment will give it to you.  It's also quite cool to build Swift and then run an actual Windows EXE.  I find it interesting and geek-exciting.

Read more about what REPL means at : Read–eval–print loop - Wikipedia[^]

That's what you'll get out of this.  Now, let's see how to get it set up.

My Assumptions For Setup

I am assuming that you have a 64 bit machine running a modern version of Windows and that you have installed Visual Studio Community 2015 and everything associated with it (C++ Redistributable and SDKs).  If you just choose the full install, you'll get it.  I was unsure if I had those too since I had installed Community a while back, but it was all there.

GitHub - Main Location

The GitHub site where you want to get the binaries we will use is at:

Release Swift for Windows (MSVC) · tinysun212/swift-windows · GitHub[^]

That link also provides the simple steps for what you need to do to set up the Swift Compiler.

I'll copy those steps here as an image so we can talk about them:

Swift Win install

Here's the direct link to pull the zip from GitHub : https://github.com/tinysun212/swift-windows/releases/download/swift-msvc-20160418/swift-msvc-20160418-bin.zip

After you get that and unzip it, simply follow each step exactly as it tells you to.

Also, notice that the last one says "Copy swift.exe to swiftc.exe in C:/Program Files/Swift/bin."

That really means rename it from swift.exe to swiftc.exe  This is the swift compiler executable.

Importance of Updating Your Path Environment Variable

That is also why you want to make sure your path (when at a console) points to this swift\bin directory.

If it does not, then when you go to compile a swift file the system will fail to find the Swiftc.exe (Swift Compiler) and your will get a command line error.

I'll let you look up how to update your path variable.  I suggest you do it at Control Panel....Sytem... (as shown in the following image):

environment vars

 

Finally, Ready To Compile

If you've successfully followed those few steps, then you are ready to compile some swift code.

Here's a first sample that will get you started:

Objective-C
let message : String = "Compiling and running Swift code on Windows is easy."

print(message)

Note: There is no choice for Swift in the CodeProject editor so I set this code to Objective-C.

Some of the Swift syntax does follow C fairly closely.  However, other syntax is unique to Swift.

Steps To Compile

  1. Save the code above to a file named 1st.swift (or download the sample file from this article).  It's best if you drop it in its own folder somewhere so files don't get mixed up with other things.
  2. Open a Visual Studio x64 Native Tools command prompt (see sidebar and next image below) at the location where you've saved the 1st.swift file.
  3. type the following command to compile the code: c:\> swiftc -c 1st.swift -o 1st.obj <ENTER>
  4. If everything works properly when you hit <ENTER> you will simply see the command prompt appear again. 
  5. Do a directory listing and you'll see that a file was created.  That file will be named : 1st.obj
  6. That file is the binary machine code that represents your Swift code.  However, it has not been linked to the machine code that will tell Windows that this is an executable.  Now, we need to run the linker.
  7. The linker is available via the Visual Studio x64 Native Tools comamnd prompt.  Now you need to run the following command to turn 1st.obj into 1st.exe which will run our code.
link /out:1st.exe 1st.obj libswiftCore.lib libswiftSwiftOnoneSupport.lib /LIBPATH:"C:/Program Files/Swift/lib/swift_static/windows" /MERGE:.rdata=.rodata /FORCE:MULTIPLE /IGNORE:4006,4049,4217

Copy that command, paste it into your command prompt and hit <ENTER>.

If everything went the way it should you will see something like:

linker console

And, you will now have a new file named 1st.exe.

#########################################################################

Sidebar : Visual Studio x64 Native Tools Command Prompt

You have to open this specific command prompt so that the associated Visual Studio tools are available to you.  In this case, it is the Linker that we need.

Notice that there are many Visual Studio command prompts. Make sure you pick the correct one.

visual studio tools command prompt

#########################################################################

Run 1st.Exe

Type 1st <ENTER> and the code will run.  You will see something like:

1st exe result

If all of that went well, you are redy to write, compile, link and run any of your Swift files.

Closer Look At Compile Command

Now that you have this all set up, let's take a closer look at the compile command so we understand what we did and how to use it for other Swift files.

For compiling you simply start the Swift compiler which is the executable named swiftc to take a file as input and product an obj file.  

You indicate the input file that you want to compile by providing a -c to swiftc and you tell it the output file by giving a -o.

Another Example Including a Swift Class

Here's some sample code (also attached to this article) where I create a Book class.

Objective-C
// book.swift

class Book{
    let author : String
    let title : String
    let pageCount : Int
    
    init (_ author : String, _ title : String, _ pageCount : Int){
        self.author = author
        self.title = title
        self.pageCount = pageCount
        print ("\(author) wrote \(title) which is \(pageCount) pages long")
    }
}

let b = Book("Matt Neuburg", "iOS 9 Programming Fundamentals with Swift", 604)

So, if we wanted to compile the attached book.swift file you would provide the following to the command line:

swiftc -c book.swift -o book.obj

Linker 

That will produce the book.obj file which needs to be linked and then we would run the linker with the following command:

c:\swiftCode>link /out:book.exe book.obj libswiftCore.lib libswiftSwiftOnoneSupport.lib /LIBPATH:"C:/Program Files/Swift/lib/swift_static/windows" /MERGE:.rdata=.rodata /FORCE:MULTIPLE /IGNORE:4006,4049,4217

Notice that this time I changed the /out: parameter to the linker to be book.exe so that my executable would be named book.exe.  I also had to provide the input obj file which I am linking and is named book.obj.

 

With these two samples you should be well on your way to writing your own Swift and compiling on Windows.

Additional Resources

If you're going to be learning pure Swift as you being learning to build iOS apps I highly suggest the following book:

iOS 9 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basics: Matt Neuburg - Amazon.com: Books[^]

You can use that book to learn core Swift syntax and coding.  It has a lot of good samples in the small that are perfect for use with this console compiler.

The Swift for Developers book is also a good one:  Swift for Programmers (Deitel Developer Series): Paul Deitel, Harvey Deitel - Amazon.com: Books[^]

History

First publication of this article and sample code: 2016-10-26

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) RADDev Publishing
United States United States
"Everything should be made as simple as possible, but not simpler."

Comments and Discussions

 
NewsNewer version is available Pin
Sangjin Han20-Jun-17 11:27
Sangjin Han20-Jun-17 11:27 
QuestionVideo tutorial of how to install swift on windows Pin
Member 1308623026-Mar-17 20:56
Member 1308623026-Mar-17 20:56 
PraiseCan learn programming without Mac Pin
JanBask Training13-Dec-16 0:24
JanBask Training13-Dec-16 0:24 
GeneralRe: Can learn programming without Mac Pin
raddevus13-Dec-16 2:09
mvaraddevus13-Dec-16 2:09 
QuestionUseful. But VPS, VM Or Hackintosh Is Still Better Way To Develop For Apple In Windows Pin
Rou199726-Oct-16 12:34
Rou199726-Oct-16 12:34 
AnswerRe: Useful. But VPS, VM Or Hackintosh Is Still Better Way To Develop For Apple In Windows Pin
raddevus27-Oct-16 2:18
mvaraddevus27-Oct-16 2:18 
GeneralRe: Useful. But VPS, VM Or Hackintosh Is Still Better Way To Develop For Apple In Windows Pin
Rou199727-Oct-16 2:31
Rou199727-Oct-16 2:31 
AnswerRe: Useful. But VPS, VM Or Hackintosh Is Still Better Way To Develop For Apple In Windows Pin
Jeremy Falcon17-Nov-16 8:15
professionalJeremy Falcon17-Nov-16 8:15 
GeneralRe: Useful. But VPS, VM Or Hackintosh Is Still Better Way To Develop For Apple In Windows Pin
raddevus17-Nov-16 8:17
mvaraddevus17-Nov-16 8:17 
GeneralRe: Useful. But VPS, VM Or Hackintosh Is Still Better Way To Develop For Apple In Windows Pin
Jeremy Falcon17-Nov-16 8:27
professionalJeremy Falcon17-Nov-16 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.