how do i say that a feature is only available on a given platform

Viewed 309

I know how to say that a dependancy is only needed on windows but how do I say (as a crate writer) that a feature is only available on windows.

I tried (based on the depends way)

[target.'cfg(windows)'.features]
windbg = []

but this doesn't work.

cargo build says

warning: unused manifest key: target.cfg(windows).features

and a client app using the crate fails saying that the feature doesn't exist

1 Answers

Currently Cargo is not able to specify feature's target platform, but you can add target_os to your code as an extra attribute to let compiler know that your feature will only be available on the target you set.

Let's say you have defined your feature like below.

#[cfg(feature = "windbg")]
mod windbg {
    //...
}

You'll need to replace it with:

#[cfg(all(target_os = "windows", feature = "windbg"))]
mod windbg {
    //...
}
Related