1

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?

antoyo
  • 11,097
  • 7
  • 51
  • 82
  • 2
    Possible duplicate of [Where should I place a static library so I can link it with a Rust program?](https://stackoverflow.com/questions/43826572/where-should-i-place-a-static-library-so-i-can-link-it-with-a-rust-program) – Stargateur Apr 28 '19 at 03:33
  • @Stargateur: No, using `cargo:rustc-link-lib=m` in a build script is the same as calling `cargo rustc -- -lm` which does not work either. – antoyo Apr 28 '19 at 03:40
  • *fetching them using `dlsym`* — doesn't that mean you need to dynamically open the library at runtime as well? See [Does Rust have a dlopen equivalent](https://stackoverflow.com/q/22461457/155423) – Shepmaster Apr 28 '19 at 15:40
  • @Shepmaster: No, `dlsym` looks into the executable as well as the dynamically linked libraries as well. – antoyo Apr 28 '19 at 15:41
  • I'd say same as in C: don't use the `-l` flag, but instead pass the library directly: `link-args=/path/to/libm.so`. – Jmb Apr 29 '19 at 06:55

1 Answers1

3

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"]
antoyo
  • 11,097
  • 7
  • 51
  • 82