如何使用 Cargo 构建多个二进制文件? [英] How can I build multiple binaries with Cargo?

查看:28
本文介绍了如何使用 Cargo 构建多个二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个带有 daemonclient 的项目,通过 unix 套接字连接.

clientdaemon 需要两个二进制文件,那么我如何告诉 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.rssrc/bin/client.rs 中,你会得到两个名为 daemonclient 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屋!

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