How to link Rust program with static library (ex. libm.a) on ARM

Has anyone been able to link an ARM static library to an ARM program ??

I'm trying to link to a cross-compiled libm.a to my embedded Rust program, but my build keeps failing :cry: Below bullet is from the tracking issue for supporting math in libcore.

  • Link to a C implementation of libm, i.e. libm.a . This is cumbersome as the programmer needs to
    obtain a compiled version of libm for their target, or compile libm themselves which implies a C
    cross toolchain when the target system and the build system are not the same architecture / OS.

I initially followed the directions specified in this stackoverflow item, but it's not working for me.
Any advice would be greatly appreciated! Thank you for checking out this post :sun_with_face:

  • The error message I'm getting
error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "--eh-frame-hdr" "-L" "/home/lonelyjoe/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/examples/c_libm-b027181ef465e490.c_libm.atfcosr3-cgu.1.rcgu.o" "-o" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/examples/c_libm-b027181ef465e490" "--gc-sections" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/deps" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/release/deps" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/build/nucleof429zi-2b54d572cec86e29/out" "-L" "/home/lonelyjoe/libm.a" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/build/cortex-m-13ac91fea5abee64/out" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/build/cortex-m-rt-71dc6d670c87db0a/out" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/build/cortex-m-semihosting-70c163116997535c/out" "-L" "/home/lonelyjoe/workspace/nucleof429zi/target/thumbv7em-none-eabihf/release/build/stm32f4-ac80701b6c1f3a23/out" "-L" "/home/lonelyjoe/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib" "-lm" "-Bstatic" "/tmp/rustcoIR9PG/libcortex_m_semihosting-ea881324b56f8c67.rlib" "/tmp/rustcoIR9PG/libcortex_m-6b9c0f008242f4e1.rlib" "/tmp/rustcoIR9PG/libcortex_m_rt-4750f6efb3e5eb0d.rlib" "--start-group" "--end-group" "/home/lonelyjoe/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/libcompiler_builtins-635853e92b6c58cf.rlib" "-Tlink.x" "-Bdynamic"
  = note: rust-lld: error: unable to find library -lm
  • The program I'm trying to compile
#![no_main]
#![no_std]

#[link(name = "m")]
extern "C" {
    fn cos(val: f64) -> f64;
}

// Halt on panic
#[allow(unused_extern_crates)] // NOTE(allow) bug rust-lang/rust#53964
extern crate panic_abort; // panic handler

use cortex_m_rt::entry;
use stm32f4xx_hal as hal;

#[entry]
fn main() -> ! {
    unsafe {
        cortex_m_semihosting::hprintln!("{}", cos(0.0)).unwrap();
        cortex_m_semihosting::hprintln!("{}", cos(1.5)).unwrap();
        cortex_m_semihosting::hprintln!("{}", cos(3.0)).unwrap();
        cortex_m_semihosting::hprintln!("{}", cos(4.5)).unwrap();
        cortex_m_semihosting::hprintln!("{}", cos(6.0)).unwrap();
    }
    loop {
        continue;
    }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.