Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Swift on Mac and run on Windows

Tags:

How can I compile a swift file on Mac as an executable to run on Windows?

All I need is an executable that can be called from C# code.

like image 205
Rodrigo Ruiz Avatar asked Mar 26 '17 18:03

Rodrigo Ruiz


People also ask

Can Swift be compiled for Windows?

Apple's Swift programming language officially landed on Windows this week, with Swift toolchain images now available for download from the open source project. Announced in a blog post on Tuesday, the release of Swift tools enables developers to build, run and debug code directly on Windows 10.

Can Xcode compile for Windows?

Given that Xcode works only on macOS, a solution to get Xcode on Windows would be to install macOS on a Windows PC by means of a virtualization app such as VMware or VirtualBox. Using a virtualization platform provides users with the full functionality of Xcode on your Windows machine.

How do I practice Swift on Windows?

While there is no built in way to program Swift in Windows 10, there is a work around. Han Sangjin has created a compiler for Swift which is available for download from Github. Download and install the Swift for Windows application using the instructions provided in the link. Once it is installed, open it up.

Can I program in Swift without Mac?

Using Xcode requires a Mac, but you can code in Swift without either! Many tutorials seem to indicate that you need a Mac with the Xcode IDE to start coding an using Swift. This is not entirely true.


2 Answers

If you have a proper swift library module for Windows, your swift compiler for macOS can generate object files for Windows, such as Hello.obj. Moreover, you can build the executable (ex. Hello.exe) if you have the special linker and some C/C++ runtime object files for Windows.

All you need one is follows.

  1. Copy the directories to the macOS from the package for Windows.

    1) Install Swift for Windows ver-1.5 (https://swiftforwindows.codeplex.com) on Windows

    2) Copy directories to macOS from Windows

     SwiftForWindows/Swift/lib/swift  // Swift library for Windows (MinGW)
     SwiftForWindows/Swift/lib/mingw  // C/C++ runtime object files (MinGW)
    
  2. Install Swift compiler for MacOS (https://swift.org/builds/swift-3.0-release/xcode/swift-3.0-RELEASE/swift-3.0-RELEASE-osx.pkg)

    You should use the same version for the library module and compiler. Use the version 3.0. Swift 3.0.1 or 3.1 will not work with SwiftForWindows-1.5.

  3. Install MinGW linker with Mac Port

    1) Install Mac Port (https:// www.macports.org/install.php)

    2) $ sudo port install x86_64-w64-mingw32-binutils

    3) Create directory bfd_ld and copy /opt/local/bin/x86_64-w64-mingw32-ld and name to ld

     $ mkdir bfd_ld
     $ cp -p /opt/local/bin/x86_64-w64-mingw32-ld bfd_ld/ld
    
  4. More Settings

    1) Rename the directory in macOS

     mv SwiftForWindows/Swift/lib/swift/mingw SwiftForWindows/Swift/lib/swift/windows
    

    2) Copy C runtime objects (3 files)

     cp SwiftForWindows/Swift/lib/mingw/*.o bfd_ld
    
  5. Compiling

     echo "print(Hello)" > Hello.swift
     ~/Library/Developer/Toolchains/swift-3.0-RELEASE.xctoolchain/usr/bin/swiftc -target x86_64-pc-windows-gnu -resource-dir SwiftForWindows/Swift/lib/swift -tools-directory bfd_ld  -L SwiftForWindows/Swift/lib/mingw -lswiftSwiftOnoneSupport -o Hello.exe Hello.swift 
    

    This will generate Hello.exe. You can ignore the 'Warning: .drectve' message.

  6. Running

    The executable file will need the DLL's in the directory 'SwiftForWindows/My Programs'.

    Copy Hello.exe to the directory in Windows and run it.

Good luck to you. Thanks.

like image 156
tinysun Avatar answered Oct 01 '22 06:10

tinysun


If you want to run Swift code on Windows, the closest you can get is swift-windows. I have never used it but it looks like you can compile Swift 3.0 code to a Windows executable.

To download pre-built binaries, try this link. It requires Windows 10 64 bit and the Visual Studio 2015 SDK. Additionally you will have to have a cygwin or mingw environment.

like image 30
Forest Kunecke Avatar answered Oct 01 '22 06:10

Forest Kunecke