如何添加外部软件包并在rust编译器中运行? [英] How to add external packages and run in rust compiler?

查看:192
本文介绍了如何添加外部软件包并在rust编译器中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rust编译并构建示例程序。我选择 rustc 代替 cargo 进行编译,因为它是一个简单的个人测试项目。到目前为止,使用 rustc 进行编译和构建可执行文件效果很好,但是当我尝试添加外部 rand 包时,它给了我错误

I am compiling and building an example program using rust. I chose rustc instead of cargo for compiling because it being a simple personal test project. So far using rustc for compiling and building executable worked fine but when I tried to add an external rand package its giving me this error

1 | extern crate rand;
  | ^^^^^^^^^^^^^^^^^^ can't find crate

这是完整代码

extern crate rand;

use rand::Rng;

fn main() {
    for x in 1..11 {
        let random_number = rand::thread_rng()
            .gen_range(1, 101);
        println!("{} -> {}", x, random_number)
    }
}

如何添加外部软件包并使用 rustc 运行?

How can I add external packages and run with rustc?

推荐答案

如果没有货运,这是可能的,但您必须做通常为您做的事情。

This is possible without Cargo, but you'll have to do what it normally does for you.


  1. 下载所有依赖项。

  2. 使用正确的标志用 rustc 编译所有依赖项。

  1. Download all the dependencies.
  2. Compile all the dependencies with rustc using the correct flags.



rand v0.7.3
├── getrandom v0.1.14
│   ├── cfg-if v0.1.10
│   └── libc v0.2.66
├── libc v0.2.66 (*)
├── rand_chacha v0.2.1
│   ├── c2-chacha v0.2.3
│   │   └── ppv-lite86 v0.2.6
│   └── rand_core v0.5.1
│       └── getrandom v0.1.14 (*)
└── rand_core v0.5.1 (*)

rand 还不错,只有8个传递依赖项(包括 rand 本身,不包括dupli类别)。不过,您仍然必须转到crates.io或github,并为每个下载正确的源代码版本。

rand isn't too bad, with only 8 transitive dependencies (including rand itself, not including duplicates). Still, you'll have to go to crates.io or github and download the correct version of the source for each.

然后即可进行编译。编译自己的二进制文件的最低要求是 rustc -Ldependent = / path / to / dependency / dir src / main.rs 。但是请记住,您必须对8个依赖项中的每一个都执行此操作,并且所有这些依赖项都有自己的外部依赖项。您还需要弄清楚编译它们的正确顺序。

Then comes compiling. The minimum you'll have to do to compile your own binary is rustc -L dependency=/path/to/dependency/dir src/main.rs. But remember that you have to do this for each of the 8 dependencies, and all of those have their own external dependencies. You'll also need to figure out the right order to compile them.

移动,某些板条箱在其 Cargo.toml中具有自己的设置。 / code>必须得到尊重。有些包装箱甚至具有需要编译和运行的构建脚本( libc 是此依赖关系树中的一个示例)。

Moveover, some crates have their own settings in their Cargo.toml that have to be respected. Some crates even have a build script that needs to be compiled and run (libc is an example in this dependency tree).

或者,您也可以放

[dependencies]
rand = "0.7.3"

并运行货物建造。你的选择。货物是Rust最好的东西之一,所以我建议您使用它。

in your Cargo.toml and run cargo build. Your choice. Cargo is one of the nicest things about Rust, so I suggest you use it.

P.S。要查看 cargo 到底在做什么,请运行 cargo clean 删除所有已编译的依赖项。然后运行货物建造--verbose (如果您很勇敢,也可以运行货物建造-vv )。您会看到所有传递给 rustc 的标志,运行的脚本以及所有其他内容。

P.S. To see what exactly cargo is doing, run cargo clean to remove any already compiled dependencies. Then run cargo build --verbose (or cargo build -vv if you're brave). You'll see all the flags that get passed to rustc, scripts that get run and everything else.

这篇关于如何添加外部软件包并在rust编译器中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆