您好,欢迎访问一九零五行业门户网

Linux平台Swift语言开发学习环境搭建

1.序言
这两天一直忙,没来得及记录东西,周三12月4日凌晨1点多看到苹果正式开源了swift,国外各大媒体资讯动作超级快。我也兴奋的起来看了一遍关于swift开源的最新消息。众所周知,苹果平台的swift语言已经出来了一年半了,一直在成长,经历了好几个版本。许多人盼望的swift开源是希望可以在除了苹果平台之外的平台可以运用这个新语言。比如有人希望将来可以用swift也可以撸一撸后台开发之类,理论上是没问题的,但是同样也有人会喷这么一个愿景,但是开源一出后,多多少少社区人员会去往这个方面去努力的。苹果官方为swift新搞了个网站swift.org,也放出来了ubuntu linux平台的预编译好的swift工具链的打包文件以及linux下的编译指南。我今天写这么一博客的目的就是为了介绍和推广swift语言在初学者或者低年级大学生群体中的运用。
2.swift+ubuntu环境配置
首先假定我们已经安装好ubuntu linux操作系统了,这个系统安装很简单,网上很多的步骤教程,虚拟机的话推荐用virtualbox。swift支持ubuntu 14.04和15.10两个发型版,我这选择15.10版本的包。
第一步:下载 swift 2.2 工具链压缩包,打开终端,输入命令新建目录并下载
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
第二步:用tar命令解压 swift 2.2 工具链压缩包到当前目录,并配置环境变量
先解压,再进入目录,目录下会有usr/bin和usr/lib等等子目录:
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/
然后配置用户级别的环境变量,编辑$home/.bashrc配置文件
diveinedu@diveinedu-virtualbox:~/swift/swift-2.2-snapshot-2015-12-01-b-ubuntu15.10$ gedit $home/.bashrc
上面命令会调出图形界面文本编辑器gedit来编辑这个配置文件,在文件的最后输入如下配置行并保存退出编辑器
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
这样环境变量就配置ok啦。这个时候我们只需要关闭我们的shell终端重新打开终端就生效了。
3.swift+ubuntu初次体验
搞过ios开发的都知道,2014年6月swift刚出世时就随xcode带了playground功能,可以边写边看运行结果,辣么在ubuntu linux下有没有类似的呢,也有,只是没那么强大的ide支持,我们一样可以运行类似pyhton脚本解析器一样的swift解析器,同步输入swift代码来“解析”运行。这个命令就是swift,在上面的环境变量设置完后重开终端就可以直接使用了,如下所示。
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
在这个解析执行界面还有自动提示补全功能!简直四国矣.上面第五行是输入hello后再输入一点.然后按tab键,一下就出来这么多关于字符串的方法,妈妈再也不担心我在终端模式下不记得方法名了。
上面这特简单的几行代码还没包含类和对象,下面看看在swift解析器中直接输入类的定义和对象创建和简单使用。
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>
这些都只是在swift解析器中临时性的运行一些代码,如果我们需要新建.swift格式文件然后编译成可执行二进制文件形式又要怎样做呢,同样很简单,我们可以用swiftc这个命令来编译。 我们可以新建一个目录来存放swift代码文件,然后编辑一个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
当打开gedit文本编辑器后,输入上面的类和对象创建以及方法调用的代码,列出在下面
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()
保存后关闭编辑器,然后执行swiftc test.swift来编译源文件,会出现如下链接错误:
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$
解决办法是安装编译依赖clang libicu-dev,输入下面命令回车(会询问当前用户密码)
diveinedu@diveinedu-virtualbox:~/swift/swiftcode$ sudo apt-get install clang libicu-dev
安装完成后再次执行编译命令swiftc test.swift就顺利编译成功,再当前目录下输出test可执行文件。
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相关的函数,程序照样可以运行,不管是脚本还是编译成二进制可执行文件,这个我以后再细说了。
其它类似信息

推荐信息