Reputation: 45
I'm trying to write a crate that only works on linux
. I have done some searches but all answers are just talking about:
#[cfg(target_os = xxx)]
in the source code[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
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