我的目标是尝试像脚本一样运行我的Swift程序.如果整个程序是自包含的,你可以运行它:
% xcrun swift hello.swift
hello.swift在哪里
import Cocoa println("hello")
但是,我想超越一步,包括swift模块,我可以导入其他类,函数等.
所以我们假设我们想要在GoodClass.swift中使用一个非常好的类
public class GoodClass { public init() {} public func sayHello() { println("hello") } }
我现在想把这个好东西导入我的hello.swift:
import Cocoa import GoodClass let myGoodClass = GoodClass() myGoodClass.sayHello()
我首先通过运行以下命令生成.o,lib <>.a,.swiftmodule:
% xcrun swiftc -emit-library -emit-object GoodClass.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name GoodClass % ar rcs libGoodClass.a GoodClass.o % xcrun swiftc -emit-module GoodClass.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name GoodClass
最后,我准备运行我的hello.swift(好像它是一个脚本):
% xcrun swift -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift
但我得到了这个错误:
:0:错误:无法加载共享库'libGoodClass'
这是什么意思?我错过了什么 如果我继续,并做链接/编译的东西类似于你为C/C++做的事情:
% xcrun swiftc -o hello -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift % ./hello
一切都很开心.我想我可以忍受,但仍然想了解共享库错误.