Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift on OS X compiling for Linux?

I'm confused by the build process for Swift on other platforms.

Does Swift allow me to build a Linux project on OS X, or do I need to use Swift specifically on Linux to to build anything I plan on using there?

I looked at the documentation, but it's not really clear on this topic...

like image 355
Milton Waddams Avatar asked Jan 27 '16 15:01

Milton Waddams


People also ask

Can Swift be compiled on Linux?

Swift is a general purpose, compiled programming language that has been developed by Apple for macOS, iOS, watchOS, tvOS and for Linux as well.

How do I run a Swift program in Linux?

Go to the Swift website and download the recommended toolchain for your version of Linux. As of this writing, Apple only supports Ubuntu, so the tutorial will use that distribution. This step installs the required dependencies and unpacks the toolchain to ~/swift . This will build and run the project.

Is it possible to run Xcode on Linux?

No. Xcode is an integrated development environment for iOS and Mac development. It is only available on OS X. (The Mac operating system).

What can you do with Swift on Linux?

Open-source Swift can be used on Linux to build Swift libraries and applications. The open-source binary builds provide the Swift compiler and standard library, Swift REPL and debugger (LLDB), and the core libraries, so one can jump right in to Swift development.


1 Answers

A pure Swift application which is not importing any framework can now be compiled for iOS, OS X and for Linux.

You will generate different executables, because it's different platforms, but the code source can be the same, it just has to be compiled for the respective platform.

The difference is when you import frameworks.

If you import UIKit to make an iOS application, obviously you won't be able to compile this on Linux, because Linux doesn't have those iOS UIKit libraries, they're only available on a Mac via Xcode.

If you import Cocoa to make an OS X application, the same logic applies, it works only for OS X.

The same way, you can make Swift code that only works on Linux if you import specific Linux libraries, like Glibc, that won't work on the Mac.

Etc, etc.

What you need to remember is that the programming language isn't that relevant when it comes to make applications for a platform, it's the frameworks and libraries that you're linked to that are decisive.

Different Swift versions come with different compilers (different toolchains, actually) so to answer directly, no you can't compile for Linux Swift with the normal OS X Swift compiler, you have to use the Swift.org's one.

like image 137
Eric Aya Avatar answered Oct 01 '22 21:10

Eric Aya