나는 녹을 배우고 간단한 클라이언트/서버 프로그램을 작성하기로 결정했습니다. 클라이언트와 서버 모두 내가 이미 작성한 아주 간단한 모듈을 사용할 것입니다. 이 코드가 커질 수 있음을 알았 기 때문에 명확성을 위해 내 소스를 분류하기로 결정했습니다. 다음과 같이 지금 내 현재의 계층 구조는 같습니다루트가 아닌 여러 바이너리에서 루트가 아닌 모듈을 가져 오기
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│ ├── client
│ │ └── main.rs
│ ├── common
│ │ ├── communicate.rs
│ │ └── mod.rs
│ ├── lib.rs
│ └── server
│ └── main.rs
Manyofthe 내가 스택 오버플로에서 찾을 예제와 그물이 main.rs
프로젝트의 루트 디렉토리에있는 경우에 좋은 샘플을 제공합니다. 불행히도 위와 같이 다른 것을하려고합니다.
communicate.rs
에는 내가 작성한 모든 네트워크 코드가 포함되어 있습니다. 결국 여기에 다른 녹 파일을 추가하고 public mod
문을 mod.rs
에 넣습니다. 현재 common/mod.rs
내가 가진 모든 그림과 같이 내가 가진 모든 main.rs
이며, 단지 client
폴더에 초점 pub mod communicate;
입니다. 파일 "헤더"나는 클라이언트 바이너리를 빌드 할 때 내가 Cargo.toml
에있는 모든
[[bin]]
name = "server"
path = "src/server/main.rs"
[[bin]]
name = "client"
path = "src/client/main.rs"
이며, 기본 [package]
섹션 게다가
extern crate common;
use std::thread;
use std::time;
use std::net;
use std::mem;
use common::communicate;
pub fn main() {
// ...
}
나열, 컴파일러가 불평하던 것이 common
상자는 할 수 없었다 찾을 수 있습니다.
$ cargo build
Compiling clientserver v0.1.0 (file:///home/soplu/rust/RustClientServer)
client/main.rs:1:1: 1:21 error: can't find crate for `common` [E0463]
client/main.rs:1 extern crate common;
^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
error: Could not compile `clientserver`.
To learn more, run the command again with --verbose.
나는 그것이 client/
폴더 내의 공통 상자를 찾고 있기 때문이라고 생각합니다. extern crate
문 대신 mod
문을 사용했을 때도 동일한 문제가 발생했습니다. 내용이 pub mod common;
하지만 난 여전히 처음과 같은 오류를 얻을 client
에 lib.rs
를 추가
client/main.rs:6:5: 6:11 error: file not found for module `common`
client/main.rs:6 mod common;
^~~~~~
client/main.rs:6:5: 12:11 help: name the file either common.rs or common/mod.rs inside the directory "client"
가 나는 또한합니다 (extern crate...
사용) 시도 :
use std::thread;
use std::time;
use std::net;
use std::mem;
mod common;
나를했다.
this project과 같은 모델을 발견했는데, 모든 폴더에 Cargo.toml
이 필요 했으므로 피해야합니다.
나는 가깝지만 느껴지는 뭔가를 느끼고 있습니다.
완벽하니, 내 문제가 해결되었습니다. 귀하의 첫 성명서는 실제로 제가 생산하려고 한 것을 정리하는데 많은 도움이되었습니다. 고맙습니다. 같은 방법으로 여러 라이브러리를 선언 할 수 있습니까? '[lib]' 'name = "otherlib"' 'path = "src/otherlib/lib.rs"' – soplu
카고는 패키지 당 하나의 라이브러리 만 지원합니다. [path dependencies] (http://doc.crates.io/specifying-dependencies.html#specifying-path-dependencies)를 사용하여 Cargo.toml의 로컬 패키지를 참조 할 수 있습니다. –