Building a learning environment for Swift language development on Linux platform
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
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/
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
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
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
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>
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
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()
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$
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
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
而且执行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)
细心的读者会发现好像不见main函数或者main相关的函数,程序照样可以运行,不管是脚本还是编译成二进制可执行文件,这个我以后再细说了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.
