How to fix "error: could not find native static library `c`, perhaps an -L flag is missing?" or how do I compile rust binaries for openwrt?

Viewed 543

I'm wanting to compile binaries for openwrt 19.07.7 on mips with limited space.

 OpenWrt 19.07.7, r11306-c4a6851c72
 -----------------------------------------------------
 root@OpenWrt:~# df /
 Filesystem           1K-blocks      Used Available Use% Mounted on
 overlayfs:/overlay        3392      1276      2116  38% /
 root@OpenWrt:~# ls -s
   662 crosshello     3 helloworld
 root@OpenWrt:~# ldd --version
 musl libc (mips-sf)
 Version 1.1.24

Above you can see the size of crosshello (cross compiled using rust) and helloworld (cross compiled using gcc). There is limited space on the target as you can see.

I followed https://github.com/japaric/rust-cross and it works. However I had to reduce the size of the binary to get it to fit. Following https://github.com/johnthagen/min-sized-rust I got the binary (the "Cross compiling with cargo" hello world with clap from the first link) to fit. I haven't compiled my own program yet. I got stuck a the "Optimize libstd with build-std" section.

I'm getting the above error:

$ cargo +nightly build -Z build-std=std,panic_abort --target=mips-unknown-linux-musl --release
   Compiling libc v0.2.106
...
error: could not find native static library `c`, perhaps an -L flag is missing?

error: could not compile `libc` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed

How do I fix that? I don't want to static link against the system libc, I want it dynamically linked like other binaries on that platform. But not sure how to do that. If I use the standard libstd the binaries are dynamically linked just fine.

Alternatively I could try nostd but my code would need significant modification and I'm not sure that would fix this problem anyway. Alternatively I could just go ahead compile my actual project with libstd and it might fit. However my project is doing data logging, so the binary is competing for space with data I want to store, so I want to minimize space as much as possible.

I've also seen reference to cargo-bloat so I'll probably check that out to find what all the space is being used for in the binary.

I'm open to different strategies to achieve what I want if anyone has ideas?

On my dev system:

$ cat .cargo/config 
[target.mips-unknown-linux-musl]
ar = "/home/alex/projects/openwrt-sdk-19.07.7-ath79-generic_gcc-7.5.0_musl.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-7.5.0_musl/bin/mips-openwrt-linux-ar"
linker = "/home/alex/projects/openwrt-sdk-19.07.7-ath79-generic_gcc-7.5.0_musl.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-7.5.0_musl/bin/mips-openwrt-linux-gcc"
$ cat Cargo.toml 
#cargo-features = ["strip"]

[package]
name = "crosshello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "3.0.0-beta.5"

[profile.release]
#strip = true
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"

(The binaries are stripped, currently I'm stripping them manually.)

0 Answers
Related