Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % cat greetings.swift
- public func sayHello(name: String) {
- print("Hello, " + name)
- }
- % cat input.swift
- import Foundation
- public func getInput() -> String {
- let stdin = NSFileHandle.fileHandleWithStandardInput()
- let data = stdin.availableData
- return NSString(data: data, encoding: NSUTF8StringEncoding) as! String
- }
- % cat main.swift
- // This file must be named main.swift; the compiler won't allow top-level statements
- // otherwise
- import greetings
- print("What is your name?")
- let name = getInput()
- sayHello(name)
- # compiles greetings.swift and input.swift into a single "library" greetings.o
- % swift -frontend -c -module-name greetings -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -g -enable-testing -emit-dependencies -emit-module -emit-module-path greetings.swiftmodule -enable-objc-interop -emit-objc-header -emit-objc-header-path greetings-Swift.h -c greetings.swift input.swift
- # compiles main.swift into main.o
- % swift -frontend -c -I . -c -primary-file main.swift -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -g -enable-testing -emit-dependencies -enable-objc-interop main.swift
- # links the final binary, automatically pulling in swift library dependencies
- # note: the automatic swift library dependency system does NOT work if
- # greetings.o is in a standard libgreetings.a AR archive and pulled in with -lgreetings
- # (and it appears there are circular dependencies in these libraries so linking
- # them manually is not fun)
- % clang -isysroot `xcrun --sdk macosx --show-sdk-path` -mmacosx-version-min=10.11 -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx -Xlinker -force_load_swift_libs -lswiftRuntime -lc++ -framework Foundation -Xlinker -add_ast_path -Xlinker greetings.swiftmodule -o hello main.o greetings.o
- % ./hello
- What is your name?
- Ben
- Hello, Ben
- % otool -L ./hello
- ./hello:
- /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
- /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1252.0.0)
- /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
- /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1253.0.0)
- /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 600.0.0)
- /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
Advertisement
Add Comment
Please, Sign In to add comment