I am writing some kind of quick entry-level code for Linux as a training exercise.
As a general task, I want to use a third-party Swift module in my own code. Let me call this module "Foo". The Foo module has a Package.swift file, and after running swift build in this directory, it created .build/debug/libFoo.so .
Now I want to do two things with this:
- Be able to
import Foo in REPL. - Be able to import Foo into my own fast program, possibly by linking to this shared object.
I have a feeling that both tasks are interconnected, so at the moment they are on the same issue.
For 1., I do not understand how packages become REPL "finds". I tried swift -F .build/debug -framework Foo , but I get a "no such module" error. I also tried swift -I .build/debug with the same result.
In 2. I examined swiftc --help and there are options -L and -L , however I could not find the correct way to use them:
$ swiftc main.swift -L ../foo.git/.build/debug -llibFoo.so main.swift:1:8: error: no such module 'Foo' import Foo ^
I use both / or Swift 2.2 or 3.0 (instead of swift build instead of swift build for <2.2>, as there is no swift build ), but it gives the same result that I think).
Please note that I understand that swift build can automatically load and create a third-party module, but I would like to know how to enable modules on disk, as they may be my own incomplete modules.
EDIT: I tried experimenting a bit with swift3 based on the discovery that you can use local paths as the url: parameter in the Package dependencies: list, at least for local development.
I created the Bar directory and Bar/Package.swift :
import PackageDescription let package = Package(name: "Bar")
I also created Bar/Sources/bar.swift containing:
public func bar(arg: Int) -> Int { return arg * 2 }
The Bar module is supposed to provide a function called bar(arg:) .
I did git init , git add . , git commit -m "Initial commit." and then git tag 1.0.0 to create a tagged local git repository for this module.
Then at the top level, I created the Foo directory and Foo/Package.swift :
import PackageDescription let package = Package( name: "Foo", dependencies: [ .Package(url: "../Bar", majorVersion: 1) ] )
Note the relative path for ../Bar .
I also created Foo/Sources/main.swift :
import Bar print(bar(arg: 11))
Now that I swift build inside Foo , it clones the Bar and builds it. However, then I get the following error; there is no such module :
$ 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
Oddly enough, if I do the same build command again, I get a different error:
$ 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
I was hoping this might work.