How to Compile "Hello World" Program with Swift on Ubuntu 14.04

Swift on Linux: Make very first step work

I had the exact same problem. It turns out that I had added the ppa:ubuntu-toolchain-r/test repo in order to install g++-4.9 on my Mint distro (17.2). Once I purged the repository and restored various libraries to their original versions, swift finally worked for me.

Specifically, I had to run

sudo apt-get install ppa-purge
sudo ppa-purge -d trusty ppa:ubuntu-toolchain-r/test

While cleaning up, ppa-purge was complaining that in order to resolve conflicts, it would have to remove quite a few packages it could not find in the Ubuntu Trusty repo (including really core ones like build-essential, xorg, gcc, x11-xserver-utils...), so I made a note and reinstalled these right away after the purge. Just be very careful.

I think some of the libraries overridden when installing g++ 4.9 were creating a conflict. I've verified all this on a fresh Mint install too.

Error with swift build in Ubuntu 14.04

This is because swift build expects to find a main.swift file in the Sources folder.

Rename your testregex.swift file to main.swift and it will build properly.

You can have as many .swift files as you want in the Sources folder, but there has to be one main.swift file.

Can not compile hello word program of Swift

This kind of errors (bash/zsh: error while loading shared libraries: ...) appear when you don't have a library installed on your system.

In your case, your missing library is libtinfo, on its version 5. You should be able to search and install it throught your system's package manager.

Just as an example, in Ubuntu you can install it by:

sudo apt install libtinfo5

Compile C code and expose it to Swift under Linux

If you build a library out of your C code, you can create a system module for it, which can then be imported into Swift, see this answer: Use a C library in Swift on Linux.

Another way to approach this task is to create a bridging header, as suggested by @Philip. Here is an oversimplified example. Let's consider the following C code:

/* In car.h */
int getInt();

/* In car.c */
int getInt() { return 123; }

We will use car.h as the bridging header. The swift source is (in file junk.swift):

print("Hi from swift!")
var i = getInt()
print("And here is an int from C: \(i)!")

First, create an object file, car.o, from car.c:

gcc -c car.c

Now build an executable, junk, as follows:

swiftc -import-objc-header car.h junk.swift car.o -o junk

Running the executable gives:

$ ./junk
Hi from swift!
And here is an int from C: 123!

The -import-objc-header option is hidden. To see it and a bunch of other hidden options, run:

swiftc -help-hidden 

I did this using Swift 3.0 development snapshot for Ubuntu 14.04 from April 12, available here: https://swift.org/builds/development/ubuntu1404/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz

Now, if you want to use C++, you will need to create a wrapper, written in a C++ source file and compiled with a C++ compiler, but with functions callable from C by using extern "C". Those functions can then be called from Swift as any C function. See, for example, this answer: Can I mix Swift with C++? Like the Objective - C .mm files

Xilinx error while compiling hello world, missing library - Ubuntu 14.04

It seems like installing the package rpm fixed the problem - honestly i don't know why.

Compile Swift script with static Swift core library

The argument -static-stdlib must be used:

swiftc -static-stdlib helloworld.swift

For swift build, pass it to -Xswiftc:

swift build -Xswiftc -static-stdlib

If you get an ICU dependency error, install it:

apt-get install libicu-dev

Swift build on Ubuntu: Handling whitespace in object file names

I didn't find a direct solution, but a way to get it working. Maybe it'll help someone with the same problem:

Make sure to use Swift version 4.2.2 (and not e.g. 4.2.0). Somehow they obviously fixed this problem between those versions. ‍♂️



Related Topics



Leave a reply



Submit