Module xcrun swift on命令行generate<;未知>;:0:错误:无法加载共享库
我的目标是尝试像脚本一样运行我的Swift程序。如果整个程序是自包含的,您可以像以下方式运行它:Module xcrun swift on命令行generate<;未知>;:0:错误:无法加载共享库,module,swift,xcrun,Module,Swift,Xcrun,我的目标是尝试像脚本一样运行我的Swift程序。如果整个程序是自包含的,您可以像以下方式运行它: % xcrun swift hello.swift 你好,斯威夫特在哪里 import Cocoa println("hello") 然而,我想再多走一步,包括swift模块,在这里我可以导入其他类、函数等 让我们假设我们有一个非常好的类,我们想在GoodClass.swift中使用它 public class GoodClass { public init() {} publi
% xcrun 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 swiftc -o hello -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift
% ./hello
然后一切都很快乐。我想我可以接受这一点,但仍然想理解共享库错误。这里是一个重新格式化的简化bash脚本,用于构建您的项目。不需要使用
-emit object
,也不需要进行后续转换。您的命令不会生成libGoodClass.dylib文件,这是在运行xcrun swift-I./“-L./”-lGoodClass-sdk$(xcrun--show sdk path--sdk macosx)hello.swift
时链接器需要的-lGoodClass
参数。您还没有使用-模块链接名称指定要链接的模块
这对我很有用:
#!/bin/bash
xcrun swiftc \
-emit-library \
-module-name GoodClass \
-emit-module GoodClass.swift \
-sdk $(xcrun --show-sdk-path --sdk macosx)
xcrun swift -I "." -L "." \
-lGoodClass \
-module-link-name GoodClass \
-sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift
#!/bin/bash
xcrun swiftc \
-emit-library \
-module-name GoodClass \
-emit-module GoodClass.swift \
-sdk $(xcrun --show-sdk-path --sdk macosx)
xcrun swift -I "." -L "." \
-lGoodClass \
-module-link-name GoodClass \
-sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift