Linux command
swiftc 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Compile a Swift source file
swiftc [file.swift]
Compile with a custom output name
swiftc -o [program] [file.swift]
Compile multiple source files
swiftc [main.swift] [utils.swift] -o [program]
Compile with debugging symbols
swiftc -g [file.swift] -o [program]
Compile with optimization
swiftc -O [file.swift] -o [program]
Show verbose compilation output
swiftc -v [file.swift]
Compile with size optimization
swiftc -Osize [file.swift] -o [program]
Emit object file only
swiftc -c [file.swift] -o [file.o]
说明
swiftc is the Swift compiler that translates Swift source code into executable binaries, libraries, object files, and modules. It is the command-line interface for building Swift programs outside of Xcode. The compiler driver coordinates multiple compilation phases: parsing, type checking, optimization, and code generation. For single-file programs, swiftc produces an executable directly. Multiple source files are compiled and linked together. swiftc is a symbolic link to the swift executable, which determines its behavior based on how it is invoked. The Swift Package Manager typically handles compilation for complex projects.
参数
- -o _file_
- Specify output file name
- -c
- Compile without linking (produce object file)
- -g
- Generate debugging symbols
- -O
- Compile with optimizations
- -Osize
- Compile with optimizations, prioritizing smaller binary size
- -Ounchecked
- Compile with optimizations, removing runtime safety checks
- -Onone
- Compile without optimizations (default for debug)
- -whole-module-optimization
- Optimize across the entire module at once
- -target _triple_
- Generate code for the given target triple (e.g., x86_64-apple-macosx10.15)
- -v, --verbose
- Show verbose output including executed commands
- -emit-executable
- Generate an executable (default when linking)
- -emit-library
- Generate a dynamic library
- -emit-module
- Generate a Swift module
- -D _flag_
- Define a conditional compilation flag
- -I _path_
- Add import search path
- -L _path_
- Add library search path
- -l _library_
- Link with specified library
- -h, --help
- Display help information
FAQ
What is the swiftc command used for?
swiftc is the Swift compiler that translates Swift source code into executable binaries, libraries, object files, and modules. It is the command-line interface for building Swift programs outside of Xcode. The compiler driver coordinates multiple compilation phases: parsing, type checking, optimization, and code generation. For single-file programs, swiftc produces an executable directly. Multiple source files are compiled and linked together. swiftc is a symbolic link to the swift executable, which determines its behavior based on how it is invoked. The Swift Package Manager typically handles compilation for complex projects.
How do I run a basic swiftc example?
Run `swiftc [file.swift]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -o _file_ do in swiftc?
Specify output file name