使用switfc终端命令编译运行swift程序

应公司的要求,要我研究swift语言,然后给大家进行swift技术培训,买了4,5本swift相关的书籍就开始研究了.

今天来介绍一下,swift相关的终端的命令.

1.首先我们在桌面上建一个swift文件夹用来存放.swift源文件,

打开终端输入cd 加空格 拖拽文件夹到终端(使用命令 ls ,cd 目录也是等效)

cd /Users/mac/Desktop/swfit2.要创建和编译运行swift源文件,需要使用 swiftc 命令,并且配以相关的options参数如下,我们查看一下,swiftc 的相关参数和用法输入  swiftc –help可以看到如下的参数的说明,大家可以根据后面的注释来使用swiftc命令yangbindeMacBook-Air:swfit mac$ swiftc –helpUSAGE: swiftc [options] <inputs>MODES: -dump-astParse and type-check input file(s) and dump AST(s) -dump-parseParse input file(s) and dump AST(s) -emit-assembly Emit assembly file(s) (-S) -emit-bcEmit LLVM BC file(s) -emit-executable Emit a linked executable -emit-irEmit LLVM IR file(s) -emit-library Emit a linked library -emit-objectEmit object file(s) (-c) -emit-silgenEmit raw SIL file(s) -emit-silEmit canonical SIL file(s) -parseParse input file(s) -print-astParse and type-check input file(s) and pretty print AST(s)OPTIONS: -application-extension Restrict code to those available for App Extensions -assert-config <value> Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement. -D <value>Specifies one or more build configuration options -emit-dependenciesEmit Make-compatible dependencies files -emit-module-path <path>Emit an importable module to <path> -emit-moduleEmit an importable module -emit-objc-header-path <path>Emit an Objective-C header file to <path> -emit-objc-headerEmit an Objective-C header file -framework <value>Specifies a framework which should be linked against -F <value>Add directory to framework search path -gEmit debug info -helpDisplay available options -import-underlying-moduleImplicitly imports the Objective-C half of a module -I <value>Add directory to the import search path -j <n>Number of commands to execute in parallel -L <value>Add directory to library link search path -l<value>Specifies a library which should be linked against -module-cache-path <value>Specifies the Clang module cache path -module-link-name <value>Library to link against when using this module -module-name <value> Name of the module to build -nostdimportDon't search the standard library import path for modules -OnoneCompile without any optimization -OuncheckedCompile with optimizations and remove runtime safety checks -output-file-map <path> A file which specifies the location of outputs -OCompile with optimizations -o <file>Write output to <file> -parse-as-libraryParse the input file(s) as libraries, not scripts -parse-silParse the input file as SIL code, not Swift source -parseable-outputEmit textual output in a parseable format -save-tempsSave intermediate compilation results -sdk <sdk>Compile against <sdk> -serialize-diagnostics Serialize diagnostics in a binary format -target-cpu <value>Generate code for a particular CPU variant -target <value>Generate code for the given target -versionPrint version information and exit -vShow commands to run and use verbose output -Xcc <arg>Pass <arg> to the C/C++/Objective-C compiler -Xlinker <value>Specifies an option which should be passed to the linker3.使用vi 命令创建一个 HelloWorld.swift源文件

进入swift文件夹后,然后使用如下的命令创建

vi HelloWorld.swift然后在文本中创建代码,打印出一些语句println("Hello这是我的第一个swift程序!")然后 按一次Esc 键, shfit +分号键,接着 输入wq ,回车

保存并且推出vi

4.然后,在swift目录下,编译 swift源文件,使用swiftc -o 参数

swiftc -o Hello.out HelloWorld.swift可以看到在 swift目录下生成了一个 .out后缀的文件

Hello.out是输出文件的类型,是mac上可以直接执行的程序,可以使用终端运行

例如,直接拖拽该.out文件到终端,然后回车可以看到终端执行了该文件并且输出结果如下:

Hello 这是我的第一个swift程序!也可以在swift目录下,直接使用 ./Hello.out来执行,效果等价./Hello.out注意你在当前目录swift下,直接使用Hello.out ,是不会执行的,因为 系统不会在当前目录下查找yangbindeMacBook-Air:swfit mac$ Hello.out-bash: Hello.out: command not foundyangbindeMacBook-Air:swfit mac$

这样就完成了,使用终端编译swift源程序,[OS X平台]

当然我们可以使用playground来替代一些简单的程序的测试

原文来自

,每天告诉自己我很棒!

使用switfc终端命令编译运行swift程序

相关文章:

你感兴趣的文章:

标签云: