简体   繁体   English

如何指定 Cargo 仅在特定目标上构建二进制文件?

[英]How do I specify that Cargo should build binaries only on a certain target?

I have a Cargo project set up with a library and a binary.我有一个带有库和二进制文件的 Cargo 项目。 The library is meant to be used on many platforms, including Android, while the binary is only meant to be used on Linux.该库旨在用于许多平台,包括 Android,而二进制文件仅用于 Linux。 As such, the binary contains a bunch of Linux-specific code that does not compile when I target Android.因此,二进制文件包含一堆特定于 Linux 的代码,当我以 Android 为目标时,这些代码无法编译。 Is there a way to specify (without using features) that the binary should only be compiled on Linux?有没有办法指定(不使用特性)二进制文件只能在 Linux 上编译?

I tried putting #![cfg(target_os = "linux")] in the main.rs of my binary, but then I got this error:我尝试将#![cfg(target_os = "linux")]放在我的二进制文件的main.rs中,但随后出现此错误:

error[E0601]: `main` function not found in crate `server`
  --> src/bin/server/main.rs:1:1
   |
1  | / #![cfg(target_os = "linux")]
2  | |
3  | | use anyhow::{self, Context};
4  | | 
...  |
36 | |     }
37 | | }
   | |_^ consider adding a `main` function to `src/bin/server/main.rs`

The below link has related information that should help you in terms of target selection and selective compilation https://doc.rust-lang.org/cargo/commands/cargo-build.html#target-selection下面的链接有相关信息,在目标选择和选择性编译方面应该对您有所帮助https://doc.rust-lang.org/cargo/commands/cargo-build.html#target-selection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM