Conditionally compilation based on target OS

Is there a way to include a crate in Cargo.toml but have that compiled conditionally based on the target OS. Say I have a crate xyz but I want to compile it only on Linux and not on Mac for instance. Can this be done? Any pointers? I know how to do this for specific functions.

Are you looking for platform specific dependencies?

Add #![cfg(target_os = "linux")] to your crate root. The crate will compile on non-linux targets, but nothing in the crate will be compiled.

Yes. This is what I did:

xyz = "1.0"

abc = "1.0"

However if I have any crate below it that doesn't get picked either. So should this be the very last item in the toml file? For instance "abc" doesn't get compiled.

That's just how TOML works. Each [...] thing starts a new section, and any lines after it will be added to that section.

So if you want abc to be added to the general [dependencies] section, you might do something like this:

name = "my-crate"

abc = "1.0"

xyz = "1.0"

pretty-assertions = "1"

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.