Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libc::write not found in libc #55

Open
alisomay opened this issue Jan 9, 2022 · 4 comments
Open

libc::write not found in libc #55

alisomay opened this issue Jan 9, 2022 · 4 comments

Comments

@alisomay
Copy link

alisomay commented Jan 9, 2022

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
Copy link
Owner

mmastrac commented Jan 12, 2022

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
Copy link

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants