Configurable link to native lib?

I currently have code that links to a native lib like so:

#[link(name = "LibName")]

Is there a way to make the link name configurable at build time? So, for example, three libs may contain the same group of functions and I want users of my crate to be able to choose which of the three to link against at build time.

#[cfg_attr(feature = "liba", link(name = "LibA"))]

Thanks! But features aren’t mutually exclusive are they? So what happens if someone were to set “liba” and “libb”?

Then rustc will try to link to both libs. You may add a pretty error when multiple features are set:

#[cfg(all(features = "liba", features = "libb"))
const PLEASE_DO_NOT_USE_LIBA_AND_LIBB_FEATURES_AT_THE_SAME_TIME: () = 0;

There is macro for that.

Thanks @bjorn3 and @jonh. So that gives me:

#[cfg(all(feature = "liba", feature = "libb"))]
compile_error!("Please do not use liba and libb features at the same time.");
1 Like

Forgot about that one.

Don’t use the attribute and specify the library in a build script.

2 Likes