Building Module for REPL


I've been trying to build library using SwiftPM.

I used a project fresh from swift package init --type=library and only changed main.swift:

// main.swift
public struct SomeStruct {
    public var text = "Hello, World!"

    public init() { }

I then build the project and run Swift REPL by invoking:

swift build -c release
swift -I/path/to/proj/.build/x86_64-apple-macosx10.10/release

In REPL, I tried to create SomeStruct but failed with error.

Welcome to Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1). Type :help for assistance.
  1> import SomeLibrary
  2> SomeStruct()
error: Couldn't lookup symbols:
  SomeLibrary.SomeStruct.init() -> SomeLibrary.SomeStruct

Thank you in advance for any help!
I'm using Swift 4.2.1 installed with Xcode on macOS 10.14.2

(Jeremy David Giesbrecht) #2

See the blog post, “REPL Support for Swift Packages”. Note that it is not part of any stable release yet.

If you really need it now, you can reverse‐engineer what the package manager does. The relevant parts begin here.

(Marc Rasi) #3

I've done this manually a few times, so I can describe what I did. (It seems to be the same as what SwiftPM does in the link that Jeremy posted).

First, add type: .dynamic to the library product in your Package.swift so that it looks like this:

            name: "package",
            type: .dynamic,
            targets: ["package"]),

Then, build it. Finally, run swift -I/path/to/proj/.build/x86_64-apple-macosx10.10/release -L/path/to/proj/.build/x86_64-apple-macosx10.10/release -lpackage. (Replace "package" with whatever appears in the name: field of the .library product).