我正在Linux上编写一些入门级的swift代码作为学习练习.
作为一般任务,我希望在我自己的代码中使用第三方Swift模块.我们称这个模块为“Foo”. Foo模块有一个Package.swift文件,在该目录中运行swift build之后,它创建了.build / deBUG / libFoo.so.
现在我想做两件事:
>能够在REPL中导入Foo.
>能够在我自己的swift程序中导入Foo,可能是通过链接这个共享对象.
我觉得这两个任务都是相关的,所以现在它们都是同一个问题.
对于1.,我不明白包裹如何被REPL“找到”.我尝试了swift -F .build / deBUG -framework Foo,但我得到了“没有这样的模块”错误.我也尝试了swift -I .build / deBUG,结果相同.
对于2.,我检查了swiftc –help并且有-L和-l选项但是我无法找到正确的方法来使用它们:
$swiftc main.swift -L ../foo.git/.build/deBUG -llibFoo.somain.swift:1:8: error: no such module 'Foo'import Foo ^
我正在使用/ Swift 2.2或3.0(使用游泳而不是快速构建2.2,因为没有快速构建 – 但它产生相同的输出我相信).
请注意,我了解swift build可以自动下载和构建第三方模块,但是我想知道如何合并磁盘模块,因为它们可能是我自己的工作进度模块.
编辑:我尝试了一个基于发现的swift3的小实验,你可以使用本地路径作为Package的依赖关系:列表中的url:参数,至少对于本地开发.
我创建了一个目录bar和bar / Package.swift:
import PackageDescriptionlet package = Package(name: "bar")
我还创建了bar / Sources / bar.swift,其中包含:
public func bar(arg: Int) -> Int { return arg * 2}
目的是模块bar提供名为bar(arg :)的函数.
我做了一个git init,git add.,git commit -m“初始提交.”然后git tag 1.0.0为这个模块创建一个标记的本地git repo.
然后回到顶层,我创建了目录Foo和Foo / Package.swift:
import PackageDescriptionlet package = Package( name: "Foo",dependencIEs: [ .Package(url: "../bar",majorVersion: 1) ])
请注意../bar的相对路径.
我还创建了Foo / Sources / main.swift:
import barprint(bar(arg: 11))
现在,当我快速构建Foo内部时,它克隆bar并构建它.然而,我得到以下错误;没有这样的模块:
$swift buildCompile Swift Module 'bar' (1 sources)Compile Swift Module 'Foo' (1 sources).../Foo/Sources/main.swift:1:8: error: no such module 'bar'import bar ^
奇怪的是,如果我再次执行完全相同的构建命令,我会得到一个不同的错误:
$swift buildCompile Swift Module 'Foo' (1 sources)linking .build/deBUG/bar.../Foo/Sources/main.swift:3:7: error: use of unresolved IDentifIEr 'bar'print(bar(arg: 11)) ^~~
我曾希望这可行.最佳答案
Be able to import Foo in my own swift program,perhaps by linking with this shared object.
使用您在“编辑”之后在问题中发布的示例,如果您使用快速构建,这似乎可以正常工作. Swift Package Manager将为您处理所有依赖项,即使它们是在磁盘上(这适用于Swift 3和4):
$cd Foo$swift buildcloning /path/to/barhead is Now at 0c3fd6e Initial commit.Resolved version: 1.0.0Compile Swift Module 'bar' (1 sources)Compile Swift Module 'Foo' (1 sources)linking ./.build/deBUG/Foo$.build/deBUG/Foo22
请注意,Foo / .build / deBUG不包含任何.so文件:
$ls Foo/.build/deBUGbar.build bar.swiftdoc bar.swiftmodule Foo Foo.build Foo.swiftdoc Foo.swiftmodule ModuleCache
我相信使用.swiftdoc和.swiftmodule文件.
Be able to import Foo in the REPL.
这部分有点麻烦,但我找到了解决方案here.要将它应用到您的示例中,您有两个选择:
> Use swift build
with extra flags(适用于Swift 3和4):
$cd bar$swift build -Xswiftc -emit-libraryCompile Swift Module 'bar' (1 sources)$swift -I .build/deBUG -L . -lbar 1> import bar 2> bar(arg: 11) $R0: Int = 22 3>
这将在当前目录中创建libbar.so:
$lslibbar.so Package.swift Sources
> Update your Package.manifest(这是特定于Swift 4):
>更新的Package.manifest看起来像这样:
// swift-tools-version:4.0import PackageDescriptionlet package = Package( name: "bar",products: [ .library( name: "bar",type: .dynamic,targets: ["bar"]),],targets: [ .target( name: "bar",dependencIEs: [],path: "Sources"),])
>这就是你如何构建并调用REPL:
$cd bar$swift buildCompile Swift Module 'bar' (1 sources)linking ./.build/x86_64-unkNown-linux/deBUG/libbar.so$swift -I .build/deBUG -L .build/deBUG -lbar 1> import bar 2> bar(arg: 11) $R0: Int = 22 3>
这将在.build / deBUG目录中创建libbar.so:
$ls .build/deBUGbar.build bar.swiftdoc bar.swiftmodule libbar.so ModuleCache
如果您无法重现这些结果,我建议清理所有.build目录和.so文件,并安装一个干净版本的Swift(我推荐swiftenv). 总结
以上是内存溢出为你收集整理的Swift on Linux – 导入第三方模块全部内容,希望文章能够帮你解决Swift on Linux – 导入第三方模块所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)