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

Viewed 1205

I have a Cargo project set up with a library and a binary. The library is meant to be used on many platforms, including Android, while the binary is only meant to be used on Linux. As such, the binary contains a bunch of Linux-specific code that does not compile when I target Android. Is there a way to specify (without using features) that the binary should only be compiled on Linux?

I tried putting #![cfg(target_os = "linux")] in the main.rs of my binary, but then I got this error:

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`
1 Answers
Related