Swift compiler for newbies

Tram Ho

How to build executable files from the Swift source file

Compile Swift source files

When you have a swift file, and you just want to run that file alone, what will you do? Do you need to create an Xcode project or playground just to run this file?

The answer is not necessary. To build and run a swift file, we can use the Swift compiler to generate a binary executable file, and then run the file. How to do it as follows: First we create a file main.swift (where you save the file, it’s up to you guys). This file simply prints a single line of text “Hello World!”.

We do not need to import the Foundation framework, because the print function is included in the Swift standard library.

Now how to turn this file into an executable file that we can run at any time?

We will have to compile this file into an executable binary file using the Swift compiler. The command we will use is swiftc .

Open terminal and cd into the folder containing the main.swift file, then enter the following command line:

You will see a new file named main appear in the folder. This is an executable file compiled from the main.swift file.

To run this file, we simply call the file name.

We can also specify the name of the output file using the -o parameter. Of course this parameter is optional, if you do not specify it, by default the compiler will take the name of the .swift file as the name of the executable file (as in the above example it is main ).

Run the following command again in the terminal:

A new executable file named hello appears in the folder. And to run this file of course just:

Arguments and flags

The swiftc statement also has many other types of flags and arguments that you can use to customize the compile process. To see a list of flags and arguments you can add -h or –help.

Custom Swift compiler flags

There may be times when you want to manually create a custom flag to specify the code in the source file you want to run. The most common example is the DEBUG flag.

Here is an example with the main.swift file:

To specify the use of a flag you need to use the -D argument. With this new main.swift file, if you run the normal swiftc command, the output will only have “Hello world!”, But if we add the DEBUG flag, the text “debug mode” will also be printed.

Or with just one command line

Compile multiple Swift files

So what if you want to compile more than 1 Swift source file into a single binary file?

It is entirely possible. We also see the example below.

First we create a file point.swift in the same directory as the main.swift file:

Now in the main.swift file, you can use this struct Point. Since both files are in the same directory, you won’t need to use keyword import.

Now, to compile both files into a single file, we just need to name both files using the swiftc command. The order of the filenames does not matter, the compiler is smart enough to determine the dependencies between files.

Run the following command in the terminal:

Similarly, if you want to create an executable file that runs in debug mode, you need to add the DEBUG flag when compiling the two swift files above:

Above is how to use the Swift compiler to create a binary executable file. Hope my article has helped you to have a more useful tool.

Share the news now

Source : Viblo