Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a swift executable for Linux on macOS

I am trying to build a swift executable on my MacBook for my Linux vServer.

I already tried using swiftc -target "x86_64-linux test.swift, but my macOS swift compiler shows this error: <unknown>:0: error: unable to load standard library for target 'x86_64--linux'

So I looked around and found this question: Swift on OS X compiling for Linux? and tried out this example script from apple to setup a cross-platform toolchain.

After trying to build a module like shown in the example text of the script, it compiled, but on my linux machine I now get this error: error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory which is strange, because swift is properly installed on my Linux machine and works.

So have I done anything wrong while cross-compiling the project, or is there a problem on my Linux machine? Also I wonder, if there is a more simple way of compiling a project for Linux on macOS, like changing the Build Settings in Xcode or something?

Thanks in advance,
Jonas

like image 260
iComputerfreak Avatar asked Jan 13 '18 17:01

iComputerfreak


People also ask

Can you compile Swift 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. Swift offers better security, performance & safety & allows us to write safe but strict code. As of now, Swift is only available for installation on Ubuntu for Linux platform.

Is it possible to run XCode on Linux?

But Apple does not provide XCode for Linux, Windows, BSD, nor any non-Mac OS. The only solution is to use AppCode (Paid), using a text editor that supports Objective-C and Swift with Swift and Objective-C compiler and debugger, install Mac OS X inside a VM, or get a cheap Mac that is powerful enough to run XCode.

How to create an executable in Swift?

A target is considered as an executable if it contains a file named main.swift . The package manager will compile that file into a binary executable. In this example, the package will produce an executable named Hello that outputs “Hello, world!”. Now run the swift package’s init command with executable type:

Can We run Swift on Linux?

The Swift program offers a safer, stronger and faster alternative for us to write secure and quality code with high performance. Swift can only be installed on Linux platforms such as Ubuntu at this time. how do i install swift? how do i download swift on ubuntu? where is swift installed? can linux run swift? can we run xcode in linux?

How to open a Swift file in Xcode?

The generated Package.swift file. We can double-click on this file to open the whole package in Xcode.

How do I run a Swift file with parameters?

Run the Swift File With Parameters The CommandLine enumeration is part of the Swift standard library to provide the command line arguments. We use CommandLine.arguments to get the parameters after the swift command. 4. Using ‘swiftc’ to Make the Executable File


1 Answers

This just means it couldn't locate the linked library. If your libswiftCore.so located at /usr/lib/swift/linux you can run LD_LIBRARY_PATH=/usr/lib/swift/linux ./<your executable for linux> and it will work like a charm.

You can also set LD_LIBRARY_PATH variable to just execute the binary.

like image 160
Devan Avatar answered Sep 22 '22 09:09

Devan