无法找到 [build-dependencies] 部分中列出的 crate
Unable to find crate that is listed in [build-dependencies] section
我尝试使用命令 cargo build
编译我的项目。
build.rs
extern crate csv;
use std::path::Path;
use std::fs::OpenOptions;
use std::io::BufWriter;
use std::io::Write;
#[allow(non_snake_case)]
fn processCSV(filename: &str, sourcePath: &str, enumName: &str) {
println!("Generate rust source code from schema {}",filename);
let mut ret: Vec<String> = Vec::new();
let mut rdr = csv::Reader::from_file(filename).unwrap().flexible(true);
for record in rdr.records().map(|r| r.unwrap()) {
}
let path = Path::new(sourcePath);
let file = match OpenOptions::new().write(true).create(true).open(&path) {
Ok(file) => file,
Err(..) => panic!("Cannot create file {}",path.display()),
};
let mut writer = BufWriter::new(file);
writer.write_all(b"test\n");
}
fn main() {
processCSV("../schemas/Test.csv", "./src/mod/common/StatusCode.rs", "StatusCode");
}
和Cargo.toml
[package]
name = "rust-test"
version = "0.0.1"
build = "build.rs"
[lib]
path = "src/lib.rs"
[dependencies]
[build-dependencies]
csv = "*"
我可以看到这个错误:
src/lib.rs:1:1: 1:18 error: can't find crate for csv
src/lib.rs:1 extern crate csv;
但是当我将 flexible(true)
更改为 flexible(false)
时,它编译得很好,没有任何错误。我需要做什么来解决这个问题?
我在 Windows 7 64 位上使用 Rust 1.2.0。
将 flexible(false)
更改为 flexible(true)
对我来说没有任何区别;都失败了。问题是您出于某种原因选择了 build-dependencies
,而不仅仅是 dependencies
.
使用您在回答中提供的 src/lib.rs 文件,以及此 Cargo.toml 文件:
[package]
name = "stack-overflow"
version = "0.1.0"
authors = ["A. Developer <a.developer@example.com>"]
[dependencies]
csv = "*"
它编译得很好。
如果您需要在您的 build.rs 和您的项目中访问一个依赖项,您需要在 both 节。
构建依赖项是 a build script 的依赖项,它是编译的辅助二进制文件,并且 运行 在构建主包之前(设计用于代码生成,并且 building/finding 本机 C 库等)。
主要代码使用的正常依赖项应该属于 "dependencies" 部分,例如
[dependencies]
csv = "0.14"
还有一个 "dev-dependencies" 部分,它们是仅在测试时需要的依赖项,即它们被编译并仅用于 cargo test
。例如,这允许 crate 依赖于 quickcheck
进行 运行ning 测试,而不会污染主要工件。
总而言之,运行ning cargo build
会做如下事情:
- 构建任何
build-dependencies
- 构建构建脚本(将编译器指向构建的构建依赖项),然后 运行 它
- 构建任何
dependencies
- 构建主包(将编译器指向构建的依赖项)
运行 cargo test
添加:
- 构建任何
dev-dependencies
- 使用
--test
构建主箱,为任何源内 #[test]
创建测试 运行ner(将编译器指向依赖项和开发依赖项)
- 构建任何外部示例或测试,同时指向依赖项和开发依赖项
我尝试使用命令 cargo build
编译我的项目。
build.rs
extern crate csv;
use std::path::Path;
use std::fs::OpenOptions;
use std::io::BufWriter;
use std::io::Write;
#[allow(non_snake_case)]
fn processCSV(filename: &str, sourcePath: &str, enumName: &str) {
println!("Generate rust source code from schema {}",filename);
let mut ret: Vec<String> = Vec::new();
let mut rdr = csv::Reader::from_file(filename).unwrap().flexible(true);
for record in rdr.records().map(|r| r.unwrap()) {
}
let path = Path::new(sourcePath);
let file = match OpenOptions::new().write(true).create(true).open(&path) {
Ok(file) => file,
Err(..) => panic!("Cannot create file {}",path.display()),
};
let mut writer = BufWriter::new(file);
writer.write_all(b"test\n");
}
fn main() {
processCSV("../schemas/Test.csv", "./src/mod/common/StatusCode.rs", "StatusCode");
}
和Cargo.toml
[package]
name = "rust-test"
version = "0.0.1"
build = "build.rs"
[lib]
path = "src/lib.rs"
[dependencies]
[build-dependencies]
csv = "*"
我可以看到这个错误:
src/lib.rs:1:1: 1:18 error: can't find crate for
csv
src/lib.rs:1 extern crate csv;
但是当我将 flexible(true)
更改为 flexible(false)
时,它编译得很好,没有任何错误。我需要做什么来解决这个问题?
我在 Windows 7 64 位上使用 Rust 1.2.0。
将 flexible(false)
更改为 flexible(true)
对我来说没有任何区别;都失败了。问题是您出于某种原因选择了 build-dependencies
,而不仅仅是 dependencies
.
使用您在回答中提供的 src/lib.rs 文件,以及此 Cargo.toml 文件:
[package]
name = "stack-overflow"
version = "0.1.0"
authors = ["A. Developer <a.developer@example.com>"]
[dependencies]
csv = "*"
它编译得很好。
如果您需要在您的 build.rs 和您的项目中访问一个依赖项,您需要在 both 节。
构建依赖项是 a build script 的依赖项,它是编译的辅助二进制文件,并且 运行 在构建主包之前(设计用于代码生成,并且 building/finding 本机 C 库等)。
主要代码使用的正常依赖项应该属于 "dependencies" 部分,例如
[dependencies]
csv = "0.14"
还有一个 "dev-dependencies" 部分,它们是仅在测试时需要的依赖项,即它们被编译并仅用于 cargo test
。例如,这允许 crate 依赖于 quickcheck
进行 运行ning 测试,而不会污染主要工件。
总而言之,运行ning cargo build
会做如下事情:
- 构建任何
build-dependencies
- 构建构建脚本(将编译器指向构建的构建依赖项),然后 运行 它
- 构建任何
dependencies
- 构建主包(将编译器指向构建的依赖项)
运行 cargo test
添加:
- 构建任何
dev-dependencies
- 使用
--test
构建主箱,为任何源内#[test]
创建测试 运行ner(将编译器指向依赖项和开发依赖项) - 构建任何外部示例或测试,同时指向依赖项和开发依赖项