Are there any cfg attributes for targetting static vs dynamic builds?

#1

Hi,

I’m working with a third-party C library, ittnotify, which is part of Intel’s solution for analyzing performance bottlenecks. One of its [mis]-features is that part of its initialization logic won’t work if it is linked to a statically-compiled libc (eg musl).

I’d like to be able to disable calling this code if my Rust project is statically compiled w/o using a Cargo feature, a bit like cfg(target_os) or cfg(target_feature). Is this possible?

Many thanks

Raph

#2

The conditional compilation page in the reference states that target_env can have the value musl, presumably when compiling for the ...-musl target. Does that work for your case? The following simple example works as expected:

#[cfg(not(target_env = "musl"))]
fn staticness() -> &'static str {
    "no"
}

#[cfg(target_env = "musl")]
fn staticness() -> &'static str {
    "yes"
}

fn main() {
    println!("Statically linked: {}", staticness());
}
#3

@inejge Thanks for the suggestion but sadly it doesn’t - whether a build is static or not is no longer a function of the choice of linux c lib (at one time, this was the case). In essence, I think I might need to switch on something like +crt-static, but I’m not sure that’s exposed.

#4

Just stumbled upon this. Maybe this helps:

#[cfg(target_feature = "crt-static")]
fn foo() {
    println!("the C runtime should be statically linked");
}

#[cfg(not(target_feature = "crt-static"))]
fn foo() {
    println!("the C runtime should be dynamically linked");
}

From: https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes

2 Likes