如何使用 Cargo 构建多个二进制文件? [英] How can I build multiple binaries with Cargo?
问题描述
我想制作一个带有 daemon
和 client
的项目,通过 unix 套接字连接.
client
和 daemon
需要两个二进制文件,那么我如何告诉 Cargo
从两个不同的来源构建两个目标?>
为了增加一点幻想,我想有一个 library
作为 daemon
的主要部分,并且只有一个二进制文件来包裹它,通过套接字进行通信.
所以,我们有这种树结构:
├── Cargo.toml├──目标|└── 调试|├── 守护进程│ └── 客户└── src├── 守护进程│ ├── bin│ │ └── main.rs│ └── lib│ └── lib.rs└── 客户└── 斌└── main.rs
我可以制作一个同时管理这两个问题的可执行文件,但这不是我想要做的,除非这是非常好的做法.
你可以使用 [[bin]]
指定多个二进制文件,正如前面提到的 此处:
[[bin]]名称 = "守护进程"path = "src/daemon/bin/main.rs"[[斌]]名称 = "客户"path = "src/client/bin/main.rs"
提示:如果你把这些文件放在 src/bin/daemon.rs
和 src/bin/client.rs
中,你会得到两个名为 daemon
和 client
as Cargo 将 src/bin
中的所有文件自动编译为同名的可执行文件.仅当您不遵循此约定时,才需要像上面的代码片段一样指定名称和路径.
I'd like to make a project with a daemon
and a client
, connecting through a unix socket.
A client
and a daemon
requires two binaries, so how do I tell Cargo
to build two targets from two different sources?
To add a bit of fantasy, I'd like to have a library
for the main part of the daemon
, and just have a binary to wrap around it and communicate through sockets.
So, we have this kind of tree architecture:
├── Cargo.toml
├── target
| └── debug
| ├── daemon
│ └── client
└── src
├── daemon
│ ├── bin
│ │ └── main.rs
│ └── lib
│ └── lib.rs
└── client
└── bin
└── main.rs
I could make one executable which manages both concerns, but that's not what I want to do, unless it's very good practice.
You can specify multiple binaries using [[bin]]
, as mentioned here:
[[bin]]
name = "daemon"
path = "src/daemon/bin/main.rs"
[[bin]]
name = "client"
path = "src/client/bin/main.rs"
Tip: If you instead put these files in src/bin/daemon.rs
and src/bin/client.rs
, you'll get two executables named daemon
and client
as Cargo compiles all files in src/bin
into executables with the same name automatically. You need to specify names and paths like in the snippet above only if you don't follow this convention.
这篇关于如何使用 Cargo 构建多个二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!