"'cargo run' 需要一个二进制目标文件"

26
在使用Intellij IDEA 2017构建一个新的Rust“来自其他来源的项目”时,我无法通过其UI运行该项目。
错误提示为:"C:/Users/sjsui/.cargo/bin/cargo.exe run error: a bin target must be available for `cargo run`",并且进程以101退出。
我注意到我的构建配置中没有提供--bin目标,因此我将项目目标文件夹的路径放置在了其中,但结果仍然相同。{{请注意保留原文中的代码格式和引用块格式}}

enter image description here

我尝试通过Cargo命令行界面创建一个新的Rust项目,但在运行时收到了以下错误信息:C:/Users/sjsui/.cargo/bin/cargo.exe run --bin C:\Users\sjsui\exercism\rust\hello-world\target\debug error: no bin target named C:\Users\sjsui\exercism\rust\hello-world\target\debug。显然,我必须安装Visual C++ Build Tools 2017,并正在进行此操作。这些错误是否相关或不同的问题?此外,当运行时出现以下错误:error: could not exec the linker link.exe: The system cannot find the file specified. (os error 2) note: the msvc targets depend on the msvc linker but link.exe was not found note: please ensure that VS 2013 or VS 2015 was installed with the Visual C++ option。
1个回答

35

默认情况下,Cargo将考虑文件src/main.rs作为包的主二进制目标。如果该文件不存在,并且在Cargo.toml中没有定义其他二进制目标,则会出现错误。

根据文档,在IntelliJ IDEA中创建Rust项目时,您可以选择使用二进制(应用程序)模板。这应该会给您一个src/main.rs而不是src/lib.rs(这是库目标的默认根文件)。通过命令行上的Cargo,您还可以使用cargo new hello创建应用程序包。

Cargo默认使用--bin来构建二进制程序。要构建库,则需要传递--lib

当您在cargo run命令上使用--bin时,该参数指向Cargo.toml中的一个[[bin]]部分,或者如果在Cargo.toml中没有[[bin]]部分,则指向遵循模式src/bin/*.rs的文件(该参数替换*)。例如,cargo run --bin foo将编译并运行src/bin/foo.rsCargo.toml中具有名字=“foo”[[bin]]部分。


我的错误是忘记了该项目是通过从源代码导入创建的(文件>新建>从其他源导入项目)。这个源只有lib.rs,就像你所说的一个库。 - Samuel Jenks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接