mmastrac / rust-libc-print

#[no_std] print equivalent for Rust
Apache License 2.0
60 stars 10 forks source link

libc::write not found in `libc` #55

Open alisomay opened 2 years ago

alisomay commented 2 years ago

Thanks for the great library. I am trying it with cargo build --target thumbv8m.main-none-eabihf

I am having these two compilation errors.

error[E0425]: cannot find function `write` in crate `libc`
  --> .../.cargo/registry/src/github.com-1ecc6299db9ec823/libc-print-0.1.17/src/lib.rs:70:15
   |
70 |         libc::write(
   |               ^^^^^ not found in `libc`
   |
help: consider importing one of these items
   |
17 | use core::fmt::write;
   |
17 | use core::ptr::write;
   |

error[E0412]: cannot find type `size_t` in crate `libc`
  --> .../.cargo/registry/src/github.com-1ecc6299db9ec823/libc-print-0.1.17/src/lib.rs:73:32
   |
73 |             msg.len() as libc::size_t,
   |                                ^^^^^^ not found in `libc`

Any ideas?

mmastrac commented 2 years ago

Are you building something bare metal? If so, it might make sense to add a feature for providing your own custom output function symbol.

BartMassey commented 2 years ago

I have this program

#![no_std]
#![feature(start)]

use libc_print::std_name::*;

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
    println!("hello");
    0
}

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
    unsafe { libc::abort() };
}

When I compile with cargo +nightly build on Debian linux (x86_64-unknown-linux-gnu) it builds and runs fine. When I compile with cargo +nightly build --target=x86_64-unknown-linux-musl I get

…
          /home/bart/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-print-0.1.20/src/lib.rs:83: undefined reference to `memcpy'
          /bin/ld: /home/bart/.rpl/lp/target/x86_64-unknown-linux-musl/debug/deps/liblibc_print-898e9466e07b75e2.rlib(libc_print-898e9466e07b75e2.libc_print.000398e2-cgu.3.rcgu.o): in function `libc_print::libc_write':
          /home/bart/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-print-0.1.20/src/lib.rs:117: undefined reference to `write'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `lp` due to previous error

When I add this build.rs

fn main() {
    println!("cargo:rustc-link-lib=c");
}

I just get the missing write

…
elf-contained/crtendS.o" "/home/bart/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: /bin/ld: /home/bart/.rpl/lp/target/x86_64-unknown-linux-musl/debug/deps/liblibc_print-898e9466e07b75e2.rlib(libc_print-898e9466e07b75e2.libc_print.000398e2-cgu.3.rcgu.o): in function `libc_print::libc_write':
          /home/bart/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-print-0.1.20/src/lib.rs:117: undefined reference to `write'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

Not sure what is up here, whether this is the same bug as above or I'm just doing something dumb. Any help appreciated!

BartMassey commented 2 years ago

Ok, figured it out. Was being hit by this open issue in Rust. Adding lto=true to my Cargo.toml and building only in release mode got me a working executable. The symptom is ultra-weird, but there we are.

kevin-valerio commented 1 year ago

Had the same issue. Downgrading to 1.0 seems to be working for me