Home php教程 PHP开发 Building a learning environment for Swift language development on Linux platform

Building a learning environment for Swift language development on Linux platform

Nov 22, 2016 pm 05:46 PM
linux

1. Preface

I have been busy these two days and haven’t had time to record anything. At around 1 am on Wednesday, December 4th, I saw that Apple officially open sourced Swift. Major foreign media information moved very quickly. I also got excited and read the latest news about Swift open source. As we all know, the Swift language for the Apple platform has been out for a year and a half, and it has been growing and going through several versions. The open source of Swift that many people are looking forward to is that this new language can be used on platforms other than the Apple platform. For example, some people hope to be able to use Swift or do background development in the future. In theory, there is no problem, but there are also people who will complain about such a vision, but once open source is released, more or less community members will go to this aspect. To work hard. Apple officially launched a new website for Swift, swift.org, and also released the pre-compiled Swift tool chain packaging files for the Ubuntu Linux platform and the compilation guide under Linux. The purpose of writing this blog today is to introduce and promote the use of Swift language among beginners or junior college students.

2.Swift+Ubuntu environment configuration

First of all, it is assumed that we have installed the Ubuntu Linux operating system. The installation of this system is very simple. There are many step-by-step tutorials on the Internet. For virtual machines, it is recommended to use VirtualBox. Swift supports two versions of Ubuntu 14.04 and 15.10. I chose the 15.10 version of the package.

Step 1: Download the Swift 2.2 toolchain compressed package, open the terminal, enter the command to create a new directory and download

diveinedu@diveinedu-VirtualBox:~$ mkdir swift && cd swift;
diveinedu@diveinedu-VirtualBox:~/swift$ wget https://swift.org/builds/ubuntu1510/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10.tar.gz
Copy after login

Step 2: Use the tar command to decompress the Swift 2.2 toolchain compressed package to the current directory, and configure environment variables

Unzip it first, and then enter the directory. There will be subdirectories such as usr/bin and usr/lib in the directory:

diveinedu@diveinedu-VirtualBox:~/swift$ tar xvf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10.tar.gz
diveinedu@diveinedu-VirtualBox:~/swift$ cd swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/
Copy after login

Then configure user-level environment variables and edit the $HOME/.bashrc configuration file

diveinedu@diveinedu-VirtualBox:~/swift/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10$ gedit $HOME/.bashrc
Copy after login

The above command will call out Use the graphical interface text editor GEdit to edit this configuration file. Enter the following configuration line at the end of the file and save and exit the editor

export SWIFT_HOME=$HOME/swift/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10
export PATH=$SWIFT_HOME/usr/bin:$PATH
export LD_LIBRARY_PATH=$SWIFT_HOME/usr/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=$SWIFT_HOME/usr/lib:$LIBRARY_PATH
Copy after login

Then the environment variables are configured OK. At this time we only need to close our Shell terminal and reopen the terminal for it to take effect.

3. First experience with Swift+Ubuntu

Everyone who has done iOS development knows that when Swift was first born in June 2014, it came with the Playground function in Xcode. You can watch the running results while writing. Is it available under Ubuntu Linux? There is nothing similar, but there is, but it does not have such powerful IDE support. We can still run a Swift parser similar to the Pyhton script parser, and input Swift code synchronously to "parse" and run it. This command is swift. After setting the above environment variables, you can use it directly by reopening the terminal, as shown below.

diveinedu@diveinedu-VirtualBox:~$ swift
Welcome to Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c). Type :help for assistance.
  1> let hello = "hello";
hello: String = "hello"
  2> let world = "diveinedu"
world: String = "diveinedu"
  3> let space = " "
space: String = " "
  4> print(hello+space+world);
hello diveinedu
  5>hello.
Available completions:
    append(c: Character) -> Void
    append(x: UnicodeScalar) -> Void
    appendContentsOf(newElements: S) -> Void
    appendContentsOf(other: String) -> Void
    characters: String.CharacterView
    debugDescription: String
    endIndex: Index
    hashValue: Int
    insert(newElement: Character, atIndex: Index) -> Void
    insertContentsOf(newElements: S, at: Index) -> Void
    isEmpty: Bool
    lowercaseString: String
    nulTerminatedUTF8: ContiguousArray<CodeUnit>
    removeAll() -> Void
    removeAll(keepCapacity: Bool) -> Void
    removeAtIndex(i: Index) -> Character
    removeRange(subRange: Range<Index>) -> Void
    replaceRange(subRange: Range<Index>, with: C) -> Void
    replaceRange(subRange: Range<Index>, with: String) -> Void
    reserveCapacity(n: Int) -> Void
    startIndex: Index
    unicodeScalars: String.UnicodeScalarView
    uppercaseString: String
    utf16: String.UTF16View
    utf8: String.UTF8View
    withCString(f: UnsafePointer<Int8> throws -> ResultUnsafePointer<Int8> throws -> Result) -> Result
    withMutableCharacters(body: (inout String.CharacterView) -> R(inout String.CharacterView) -> R) -> R
    write(other: String) -> Void
    writeTo(&target: Target) -> Void
  6> hello.isEmpty
$R0: Bool = false
Copy after login

There is also an automatic prompt completion function in this analysis and execution interface! It’s like four countries. The fifth line above is to enter hello and then enter a little more. Then press the tab key, and so many methods about strings will appear at once. My mother no longer worries that I can’t remember the method names in terminal mode.

The above simple lines of code do not include classes and objects. Let’s take a look at how to directly enter the class definition and object creation and simple use in the swift parser.

diveinedu@diveinedu-VirtualBox:~$ swift
Welcome to Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c). Type :help for assistance.
  1> struct Resolution {
  2.     var width = 0
  3.     var height = 0
  4. }
  5. class VideoMode {
  6.     var resolution = Resolution()
  7.     var interlaced = false
  8.     var frameRate = 0.0
  9.     var name: String?
 10.     func description()
 11.     {
 12.       print("name:\(name) frameRate:\(frameRate)")
 13.     }
 14. }
 15> let mode = VideoMode()
mode: VideoMode = {
  resolution = {
    width = 0
    height = 0
  }
  interlaced = false
  frameRate = 0
  name = nil
}
 16> mode.name = "1080p HD"
 17> mode.frameRate = 30.0
 18> mode.description()
name:Optional("1080p HD") frameRate:30.0
 19>
Copy after login

These are just some codes that are temporarily run in the swift parser. What if we need to create a new .swift format file and then compile it into an executable binary file? It is also very simple. We can use the swiftc command to compile. We can create a new directory to store swift code files, and then edit a test.swift:

diveinedu@diveinedu-VirtualBox:~$ mkdir -p $HOME/swift/swiftcode
diveinedu@diveinedu-VirtualBox:~$ cd  $HOME/swift/swiftcode
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ gedit test.swift
Copy after login

After opening the gedit text editor, enter the above code for class and object creation and method calling, which are listed below

struct Resolution {
    var width = 0
    var height = 0
}
class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
    func description()
    {
      print("name:\(name) frameRate:\(frameRate)")
    }
}
let mode = VideoMode()
mode.name = "1080p HD"
mode.frameRate = 30.0
mode.description()
Copy after login

Save Then close the editor, and then execute swiftc test.swift to compile the source file. The following link error will appear:

diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ swiftc test.swift
<unknown>:0: error: link command failed with exit code 127 (use -v to see invocation)
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$
Copy after login

The solution is to install the compilation dependency clang libicu-dev, enter the following command and press Enter (the current user password will be asked)

diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ sudo apt-get install clang libicu-dev
Copy after login

After the installation is completed, execute the compilation command swiftc test.swift again and the compilation will be successful, and the test executable file will be output in the current directory.

diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ swiftc test.swift
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ ./test
name:Optional("1080p HD") frameRate:30.0
Copy after login

而且执行ldd ./test查看此二进制文件依赖的动态库可知,它链接了libswiftCore,这是所有swift程序都会需要的。

diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ ldd ./test
    linux-vdso.so.1 =>  (0x00007ffcef3f5000)
    libswiftCore.so => /home/diveinedu/swift/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/lib/swift/linux/libswiftCore.so (0x00007f1cd2f75000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1cd2bdd000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1cd28d5000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1cd26be000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1cd22f3000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1cd20d5000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f1cd1ed1000)
    libicuuc.so.55 => /usr/lib/x86_64-linux-gnu/libicuuc.so.55 (0x00007f1cd1b3c000)
    libicui18n.so.55 => /usr/lib/x86_64-linux-gnu/libicui18n.so.55 (0x00007f1cd16d9000)
    libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f1cd14c9000)
    /lib64/ld-linux-x86-64.so.2 (0x0000556e488b7000)
    libicudata.so.55 => /usr/lib/x86_64-linux-gnu/libicudata.so.55 (0x00007f1ccfa11000)
Copy after login

细心的读者会发现好像不见main函数或者main相关的函数,程序照样可以运行,不管是脚本还是编译成二进制可执行文件,这个我以后再细说了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles