- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在 Linux 上编写一些入门级的 swift 代码作为学习练习。
作为一般任务,我希望在自己的代码中使用第三方 Swift 模块。我们称这个模块为“Foo”。 Foo 模块有一个 Package.swift 文件,在该目录中运行 swift build
后,它创建了 .build/debug/libFoo.so
。
现在我想用它做两件事:
导入 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.so
main.swift:1:8: error: no such module 'Foo'
import Foo
^
我正在使用 Swift 2.2 或 3.0(使用 swim
而不是 swift build
用于 2.2,因为没有 swift build
- 但我相信它会产生相同的输出)。
请注意,我了解 swift build
可以自动下载和构建第三方模块,但是我想知道如何合并磁盘上的模块,因为它们可能是我自己的工作-进度模块。
编辑:我尝试了一个 swift3 的小实验,基于一个发现,你可以使用本地路径作为包的 dependencies:
列表中的 url:
参数,至少促进本地发展。
我创建了一个目录 Bar
和 Bar/Package.swift
:
import PackageDescription
let 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 "Initial commit."
然后是 git tag 1.0.0
为这个模块创建一个标记的 local git repo。
然后回到顶层,我创建了目录 Foo
和 Foo/Package.swift
:
import PackageDescription
let package = Package(
name: "Foo",
dependencies: [ .Package(url: "../Bar", majorVersion: 1) ]
)
注意 ../Bar
的相对路径。
我还创建了 Foo/Sources/main.swift
:
import Bar
print(bar(arg: 11))
现在,当我在 Foo
中 swift build
时,它会克隆 Bar
并构建它。但是然后我收到以下错误; 没有这样的模块:
$ swift build
Compile Swift Module 'Bar' (1 sources)
Compile Swift Module 'Foo' (1 sources)
.../Foo/Sources/main.swift:1:8: error: no such module 'Bar'
import Bar
^
<unknown>:0: error: build had 1 command failures
error: exit(1): .../swift-3.0-PREVIEW-4-ubuntu14.04/usr/bin/swift-build-tool -f .../Foo/.build/debug.yaml
奇怪的是,如果我再次执行完全相同的构建命令,我会得到一个不同的错误:
$ swift build
Compile 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))
^~~
<unknown>:0: error: build had 1 command failures
error: exit(1): .../swift-3.0-PREVIEW-4-ubuntu14.04/usr/bin/swift-build-tool -f .../Foo/.build/debug.yaml
我曾希望这会奏效。
最佳答案
Be able to import Foo in my own swift program, perhaps by linking with this shared object.
使用您在“编辑”之后在问题中发布的示例,如果您使用 swift build
,这似乎可以正常工作。 Swift Package Manager将为您处理所有依赖项,即使它们在磁盘上(这适用于 Swift 3 和 4):
$ cd Foo
$ swift build
Cloning /path/to/Bar
HEAD is now at 0c3fd6e Initial commit.
Resolved version: 1.0.0
Compile Swift Module 'Bar' (1 sources)
Compile Swift Module 'Foo' (1 sources)
Linking ./.build/debug/Foo
$ .build/debug/Foo
22
请注意 Foo/.build/debug 不包含任何 .so 文件:
$ ls Foo/.build/debug
Bar.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-library
Compile Swift Module 'Bar' (1 sources)
$ swift -I .build/debug -L . -lBar
1> import Bar
2> bar(arg: 11)
$R0: Int = 22
3>
这会在当前目录中创建 libBar.so:
$ ls
libBar.so Package.swift Sources
Update your Package.manifest (这是 Swift 4 特有的):
更新后的 Package.manifest 看起来像这样:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "Bar",
products: [
.library(
name: "Bar",
type: .dynamic,
targets: ["Bar"]),
],
targets: [
.target(
name: "Bar",
dependencies: [],
path: "Sources"),
]
)
这就是构建和调用 REPL 的方式:
$ cd Bar
$ swift build
Compile 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/debug
Bar.build Bar.swiftdoc Bar.swiftmodule libBar.so ModuleCache
如果您无法重现这些结果,我建议清除所有 .build 目录和 .so 文件,并安装一个干净的 Swift 版本(为此我推荐 swiftenv)。
关于linux - Swift on Linux - 导入第三方模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39071564/
我最近在我的机器上安装了 cx_Oracle 模块,以便连接到远程 Oracle 数据库服务器。 (我身边没有 Oracle 客户端)。 Python:版本 2.7 x86 Oracle:版本 11.
我想从 python timeit 模块检查打印以下内容需要多少时间,如何打印, import timeit x = [x for x in range(10000)] timeit.timeit("
我盯着 vs 代码编辑器上的 java 脚本编码,当我尝试将外部模块包含到我的项目中时,代码编辑器提出了这样的建议 -->(文件是 CommonJS 模块;它可能会转换为 ES6 模块。 )..有什么
我有一个 Node 应用程序,我想在标准 ES6 模块格式中使用(即 "type": "module" in the package.json ,并始终使用 import 和 export)而不转译为
我正在学习将 BlueprintJS 合并到我的 React 网络应用程序中,并且在加载某些 CSS 模块时遇到了很多麻烦。 我已经安装了 npm install @blueprintjs/core和
我需要重构一堆具有这样的调用的文件 define(['module1','module2','module3' etc...], function(a, b, c etc...) { //bun
我是 Angular 的新手,正在学习各种教程(Codecademy、thinkster.io 等),并且已经看到了声明应用程序容器的两种方法。首先: var app = angular.module
我正在尝试将 OUnit 与 OCaml 一起使用。 单元代码源码(unit.ml)如下: open OUnit let empty_list = [] let list_a = [1;2;3] le
我在 Angular 1.x 应用程序中使用 webpack 和 ES6 模块。在我设置的 webpack.config 中: resolve: { alias: { 'angular':
internal/modules/cjs/loader.js:750 return process.dlopen(module, path.toNamespacedPath(filename));
在本教程中,您将借助示例了解 JavaScript 中的模块。 随着我们的程序变得越来越大,它可能包含许多行代码。您可以使用模块根据功能将代码分隔在单独的文件中,而不是将所有内容都放在一个文件
我想知道是否可以将此代码更改为仅调用 MyModule.RED 而不是 MyModule.COLORS.RED。我尝试将 mod 设置为变量来存储颜色,但似乎不起作用。难道是我方法不对? (funct
我有以下代码。它是一个 JavaScript 模块。 (function() { // Object var Cahootsy; Cahootsy = { hello:
关闭。这个问题是 opinion-based 。它目前不接受答案。 想要改进这个问题?更新问题,以便 editing this post 可以用事实和引文来回答它。 关闭 2 年前。 Improve
从用户的角度来看,一个模块能够通过 require 加载并返回一个 table,模块导出的接口都被定义在此 table 中(此 table 被作为一个 namespace)。所有的标准库都是模块。标
Ruby的模块非常类似类,除了: 模块不可以有实体 模块不可以有子类 模块由module...end定义. 实际上...模块的'模块类'是'类的类'这个类的父类.搞懂了吗?不懂?让我们继续看
我有一个脚本,它从 CLI 获取 3 个输入变量并将其分别插入到 3 个变量: GetOptions("old_path=s" => \$old_path, "var=s" =
我有一个简单的 python 包,其目录结构如下: wibble | |-----foo | |----ping.py | |-----bar | |----pong.py 简单的
这种语法会非常有用——这不起作用有什么原因吗?谢谢! module Foo = { let bar: string = "bar" }; let bar = Foo.bar; /* works *
我想运行一个命令: - name: install pip shell: "python {"changed": true, "cmd": "python <(curl https://boot
我是一名优秀的程序员,十分优秀!