Search code examples
rustlinkerffi

How to link a C library without calling one of its functions?


I'd like to link with -lm. Right now, I'm doing it this way:

let _link_lm = f64::sin(3.0);

I tried putting this in .cargo/config:

[build]
rustflags = ["-C", "link-args=-lm"]

This doesn't dynamically link the library. Also, using cargo:rustc-link-lib=m in a build script is the same as calling cargo rustc -- -lm which does not work either. I check that the library is not linked with ldd.

It matters to link the library because this is for a JIT compiler which can call these functions by fetching them using dlsym.

How I can link to this library without calling one of its functions?


Solution

  • It turns out that rustc call the linker with -Wl,--as-needed, so the solution for me was to disable this option:

    [build]
    rustflags = ["-C", "link-arg=-Wl,--no-as-needed", "-C", "link-arg=-lm"]