前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用cargo edit管理Rust项目的依赖

使用cargo edit管理Rust项目的依赖

作者头像
fliter
发布2024-03-18 16:25:12
1390
发布2024-03-18 16:25:12
举报
文章被收录于专栏:旅途散记旅途散记

介绍

大概因为IDE还不够智能&强大,在Rust中每次需要引入依赖时,都需要手工添加到Cargo.toml文件中,而且版本号还要去crates上面去查 (这个通过安装插件,可以给出提示版本)

而cargo-edit可以自动帮助添加依赖,且自动更新版本号

cargo-edit[1]是一个很好用的工具,扩展了Cargo的功能,允许通过命令行修改Cargo.toml文件来添加、移除和升级依赖。

cargo-edit包括几个子命令,如cargo upgrade、cargo add和cargo rm等

其中,cargo addcargo rm已经在最新的cargo中集成。

cargo upgrade用于将Cargo.toml中的依赖升级到其最新版本,并提供了不同的升级选项。

cargo set-version用于在Cargo.toml中设置版本号,可以通过指定版本号或自动增加主版本、次版本或修订版本来使用。

如果没有安装cargo-edit,可以通过cargo install cargo-edit 进行安装

如果有如下报错:

代码语言:javascript
复制
error: linking with `cc` failed: exit status: 1
 |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="xxxxxx(一大堆PATH路径)"
  = note: Undefined symbols for architecture arm64:
            "_iconv", referenced from:
                _git_fs_path_iconv in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
               (maybe you meant: _git_fs_path_iconv_clear, _git_fs_path_iconv_init_precompose , _git_fs_path_iconv )
            "_iconv_close", referenced from:
                _git_fs_path_direach in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_iconv_clear in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_diriter_free in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
            "_iconv_open", referenced from:
                _git_fs_path_direach in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_iconv_init_precompose in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
                _git_fs_path_diriter_init in liblibgit2_sys-d72cdefc619e6458.rlib(fs_path.o)
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `cargo-edit` (bin "cargo-upgrade") due to previous error
error: failed to compile `cargo-edit v0.12.2`, intermediate artifacts can be found at `/var/folders/9t/839s3jmj73bcgyp5x_xh3gw00000gn/T/cargo-installhdtjjF`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.

可应该是arm64上特有的问题,可按照 error: linking with `cc` failed: exit code: 1[2]来操作,

即在~/.cargo/config 中(如果没有此文件,则创建),增加如下内容:

代码语言:javascript
复制
[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

再次cargo install cargo-edit,成功安装

使用

以添加某个crate为例:

cargo add 需要的库名

再如

参考资料

[1]

cargo-edit: https://github.com/killercup/cargo-edit

[2]

error: linking with cc failed: exit code: 1: https://stackoverflow.com/questions/28124221/error-linking-with-cc-failed-exit-code-1

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 旅途散记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档