洪古格
洪古格

Reputation: 45

How to specify the target_os in `Cargo.toml`?

I'm trying to write a crate that only works on linux. I have done some searches but all answers are just talking about:

  1. add #[cfg(target_os = xxx)] in the source code
  2. add [target.'cfg(target_os = "linux")'.dependencies] in Cargo.toml.

But in my case, the crate entirely won't work in other platform so I don't want to add #[cfg] in my code. If I do so, I need to add it to every piece of my code.

Is there a easy way to make my code only compile in specific platform?

Upvotes: 3

Views: 3147

Answers (1)

eggyal
eggyal

Reputation: 125835

You can conditionally include an invocation of compile_error:

#[cfg(not(target_os = "linux"))]
compile_error!("only linux is supported");

This only needs to appear in your code once; I'd suggest somewhere near the start of the root module.

Upvotes: 4

Related Questions