Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude a file from being built on OS X?

I have src/bin/linux-only.rs which does some things which work on Linux only (e.g. libc bindings which only exist on Linux). I want to exclude that file from being built on OS X.

I started putting #[cfg(target_os = "linux")] on every block in linux-only.rs but that is cluttering up the source code beyond any reason.

Is there a nicer way to do this?

like image 480
hansaplast Avatar asked Mar 11 '23 01:03

hansaplast


1 Answers

Writing #![cfg(target_os = "linux")] (note the exclamation mark) at the top of the file will work for the whole file (as long as it contains a single module), not just for the next block (item). Source: Rust reference.

Edit: if you can move that file into its own crate, you could take advantage of Cargo's platform-specific dependencies.

like image 196
ljedrz Avatar answered Mar 23 '23 21:03

ljedrz