【问题标题】:How to link against a local Rust library? (similar to npm link)如何链接到本地​​ Rust 库? (类似于 npm 链接)
【发布时间】:2014-09-27 00:08:15
【问题描述】:

在 node 中开发库时, 如果您希望针对仅存在于本地的库进行开发, 在你之前npm publish, 你可以使用npm link /path/to/other/node_library

Rust 的等价物是什么? 你如何创建另一个链接到 bar 库的 foo 可执行文件, 不先将 bar 库推送到 git 远程?

The official rust tutorial 显示如何使用原始rustc 执行此操作, 在Cargo.toml 中如何做到这一点?

cargo documentation 向您展示了如何构建一个库, 但是现在如何链接到没有远程存储库的存储库。)

【问题讨论】:

    标签: node.js git rust rust-crates rust-cargo


    【解决方案1】:

    如果您的依赖项位于本地 git repo 中,也可以使用 git file: URL:

    [dependencies.local_dep]
    git = "file:/some/local/path"
    

    当您想使用自己的某个包的本地副本时,还有一个非常有用的功能。您可以在~/.cargo/config 文件中指定此类包的路径:

    package-name = "/path/to/package"
    

    使用此配置,当某些其他包(我们将其命名为 a)需要 package-name 时,无论 a 清单中关于 package-name 位置声明的内容如何,​​package-name 都将从指定的源代码树构建在这个配置文件中。当您需要在其他项目所依赖的库中测试您的更改时,这很有用。

    【讨论】:

    • 感谢您的回答,但是当本地依赖项和链接到它的可执行文件链接到相同的第 3 方依赖项时,这两个建议都不起作用,运行 cargo update 时出现错误: At the moment, Cargo only supports a single source for a particular package name (Dependency { name: nickel, source_id: https://github.com/nickel-org/nickel.rs.git, req: *, transitive: true, only_match_name: false }).
    • 我相信 package-name 现在是 paths 并且是根据 doc.crates.io/config.html#configuration-keys 的文档的数组
    【解决方案2】:

    你可以这样做:

    [dependencies.local_dep]
    path = "some/local/path"
    

    查看https://github.com/gfx-rs/gfx-rs/blob/master/Cargo.toml 的示例。

    【讨论】:

    • 感谢您的回答,但是当本地依赖项和链接到它的可执行文件链接到相同的第 3 方依赖项时,此建议不起作用,运行 cargo update 时出现错误:@987654325 @
    • Here 是关于 path 的官方 Cargo 文档。
    【解决方案3】:

    http://doc.crates.io/manifest.html#the-dependencies-section 包含使用 path 字段(geometry crate)执行此操作的示例:

    [package]
    # ...
    
    [dependencies]
    hammer = { version = "0.5.0", git = "https://github.com/wycats/hammer.rs" }
    color = { git = "https://github.com/bjz/color-rs" }
    geometry = { path = "crates/geometry" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多