首页 > 解决方案 > 无法释放文件。出现错误:无法编译“libc”

问题描述

我对这个语言和编码领域非常陌生。也是编码领域的初学者。我尝试构建和发布文件,但在编译 libc v0.2.62 时出错

error: Could not compile `libc`
pi@raspberrypi:~/Ganesh_Rust/Real_time/led_blink/src $ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `/home/pi/Ganesh_Rust/Real_time/led_blink/target/debug/led_blink`
pi@raspberrypi:~/Ganesh_Rust/Real_time/led_blink/src $ cargo build --release
   Compiling libc v0.2.62
error: Could not compile `libc`.

Caused by:
  process didn't exit successfully: `rustc --crate-name build_script_build /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.62/build.rs --color always --crate-type bin --emit=dep-info,link -C opt-level=3 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=b79e3ef31fa8c249 -C extra-filename=-b79e3ef31fa8c249 --out-dir /home/pi/Ganesh_Rust/Real_time/led_blink/target/release/build/libc-b79e3ef31fa8c249 -L dependency=/home/pi/Ganesh_Rust/Real_time/led_blink/target/release/deps --cap-lints allow` (signal: 11, SIGSEGV: invalid memory reference)

代码:我在 VS 代码中编写的这个程序,用于在树莓派 3 上闪烁 LED

use rust_gpiozero::*;
use std::thread;
use std::time::Duration;

fn main() {
  //create a new LEd attached to pin 17 of raspberry pi 
  let led = LED::new(17);

  //blink the led 5 times
  for _ in 0.. 5{
      led.on();
      thread::sleep(Duration::from_secs(10));
      led.off();
      thread::sleep(Duration::from_secs(10));
}
}  

cargo.toml 文件:

[package]
name = "led_blink"
version = "0.1.0"
authors = ["pi"]
edition = "2018"

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

[dependencies]
rust_gpiozero = "0.2.0"

我在 Raspberry pi 上得到输出,但可执行文件和二进制文件很大(5MB)。所以我想如果我发布也许我可以减小大小所以尝试使用 cargo build --release 命令发布,但得到这个错误。

标签: rustrust-cargo

解决方案


如果您使用的是 rustup 提供的二进制文件,那么这是一个已知问题 upstream。该问题有一个解决方法,即在中设置以下内容Cargo.toml

[profile.release]
codegen-units = 1

作为替代方案,您可以使用 Debianrustccargo软件包而不是 rustup,这应该可以正常工作。您可以从https://packages.debian.org/rustchttps://packages.debian.org/cargo下载软件包,也可以在其中添加适当的 APT 行/etc/sources.list(参见https://deb.debian. org/例如)。请注意,Debian 并不总是有最新版本,但它们应该可以工作。


推荐阅读