加载 Swift 库:"error: no such module"



newbie swift问题在这里。如果我进行2个Swift项目 - 一个可执行文件和一个库,我会从可执行文件中调用库。

a(因此,如果我们创建这样的项目:

~ $ mkdir Foo Bar
~ $ cd Foo/
Foo $ swift package init --type executable
Foo $ cd ../Bar/
Bar $ swift package init --type library
Bar $ git init .
Bar $ git add .
Bar $ git commit -m "Initial commit"
Bar $ git tag 1.0.0
Bar $ swift build

b(从这里开始,如果我尝试在" foo"中包括" bar",我会得到 error: no such module 'Bar'

这看起来像是一个快速的路径问题。因此,我当然缺少一些真正的基础。有人可以指出我缺少的东西吗?

文件:package.swift

  // swift-tools-version:4.0
  // The swift-tools-version declares the minimum version of Swift required to build this package.
  import PackageDescription
  let package = Package(
      name: "Foo",
      dependencies: [
          .package(url: "../Bar", from: "1.0.0"),
      ],
      targets: [
          .target(
              name: "Foo",
              dependencies: []),
      ]
  )

文件:来源/foo/main.swift

  import Bar
  print("Hello, world!")

Swift Build

Bar $ cd ../Foo
Foo $ swift build
Compile Swift Module 'Foo' (1 sources)
Foo/Sources/Foo/main.swift:1:8: error: no such module 'Bar'
import Bar
       ^
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f Foo/.build/debug.yaml main

版本

$ swift --version
Apple Swift version 4.0.3 
Target: x86_64-apple-macosx

,因此似乎您需要两次包含"依赖项"。一旦进入"依赖项"部分,一旦在"目标"部分中。谢谢 @user9335240。

import PackageDescription
let package = Package(
    name: "Foo",
    dependencies: [
      .package(url: "../Bar", from: "1.0.0"),
    ],
    targets: [
        .target(
            name: "Foo",
            dependencies: ["Bar"]),
    ]
)

最新更新