Statically linking ffmpeg-sys on Amazon Linux fails with undefined references - rust

My project depends on the ffmpeg-sys crate which is configured to build statically, as follows:
[dependencies.ffmpeg-sys]
version = "3.4.1"
default-features = false
features = ["avcodec", "avformat", "swresample", "build", "static"]
My project consists of a single simple file:
extern crate ffmpeg_sys;
use ffmpeg_sys::av_register_all;
fn main() {
unsafe { av_register_all() };
println!("Hello, world!");
}
When compiling with cargo build I get the following error:
Compiling sample v0.1.0 (file:///home/ec2-user/sample)
error: linking with 'cc' failed: exit code: 1 | = note: "cc"
"-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L"
[... elided for clarity ...]
In function 'sample::main::hbbb19855251826d6':
/home/ec2-user/sample/src/main.rs:6: undefined reference to 'av_register_all'
collect2: error: ld returned 1 exit status
The required static libraries, libavformat.a and friends, are found in the target/build/debug folder, showing that ffmpeg-sys successfully compiled the libraries.
Here's the rustc command that is failing:
Caused by: process didn't exit successfully: 'rustc --crate-name
sample src/main.rs --crate-type bin --emit=dep-info,link -C
debuginfo=2 -C metadata=250bf40eb277d05a -C
extra-filename=-250bf40eb277d05a --out-dir
/home/ec2-user/sample/target/debug/deps -C
incremental=/home/ec2-user/sample/target/debug/incremental -L
dependency=/home/ec2-user/sample/target/debug/deps --extern
ffmpeg_sys=/home/ec2-user/sample/target/debug/deps/libffmpeg_sys-fa3ff87f80f2d27e.rlib
-L native=/home/ec2-user/sample/target/debug/build/ffmpeg-sys-0b3c813f29a9a20e/out/dist/lib'
(exit code: 1)
libffmpeg_sys-fa3ff87f80f2d27e.rlib is 207M and I assume therefore contains all the statically compiled ffmpeg code.
This only happens when I build on an Amazon Linux instance. Compiling on my regular Fedora 28 desktop results in a working binary.
How would I go about figuring out the root cause of this error?

I solved this problem by building llvm 6.0.1 and then rebuilding with LIBCLANG_PATH set to point to the newer version.
It would appear that rustc has a minimum version requirement on libclang.so, but I could not find an official source documenting that. The version installed on amazon-linux is 3.6.2 which is evidently too old.

Related

Cross-compiling [no_std] code - libcore and compiler_builtins not found

I'm trying to follow the steps explained in Embeddonomicon to compile the smallest #![no-std] program, but for a new target on a new architecture.
The architecture is upstreamed in LLVM as an experimental target ("ARC") but its not used by rustc, so first I enabled it in the LLVM that comes with Rust as explained here: run ./x.py setup then update the config.toml:
[llvm]
download-ci-llvm = false
ninja = true
targets = "X86"
experimental-targets = "ARC"
Then I manually added the support for the arch following the steps explained here (using this commit as an example):
created rustc_target/src/abi/call/arc.rs
updated rustc_llvm/src/lib.rs
etc
Then I added the target file arc-pc-unknown-gnu.json and made it visible through RUST_TARGET_PATH envvar:
{
"arch": "arc",
"cpu": "generic",
"abi": "eabi",
"c-enum-min-bits": 8,
"data-layout": "e-m:e-p:32:32-i64:32-f64:32-v64:32-v128:32-a:0:32-n32",
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"executables": true,
"features": "",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"llvm-target": "arc-pc-unknown-gnu",
"max-atomic-width": 32,
"atomic-cas": false,
"panic-strategy": "abort",
"relocation-model": "static",
"target-pointer-width": "32"
}
Built the compiler: ./x.py build -i --target=arc-pc-unknown-gnu library/core. It finished successfully, I could see stage1 libs for the arc-pc-unknown-gnu target
I thought this would be enough, but the code was not compiling because of the following issues:
$ rustc --emit=llvm-ir -o rust_main.ll -C panic=abort --target arc-pc-unknown-gnu src/main.rs
error[E0463]: can't find crate for `core`
|
= note: the `arc-pc-unknown-gnu` target may not be installed
= help: consider downloading the target with `rustup target add arc-pc-unknown-gnu`
= help: consider building the standard library from source with `cargo build -Zbuild-std`
error[E0463]: can't find crate for `compiler_builtins`
error[E0412]: cannot find type `PanicInfo` in this scope
--> src/main.rs:18:18
This is weird because on the previous step I should have compiled these very libs for my target...
Then I though that perhaps I needed to rebuild libcore again using cargo build-std (although I don't know why exactly, but someone on the web mentioned this)? I tried this, but now there are the following errors:
$ cargo build -Z build-std=core --target arc-pc-unknown-gnu
Compiling compiler_builtins v0.1.70
Compiling core v0.0.0 (/home/valeriyk/proj/rust-arc/1.60.0/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/src/rust/library/core)
error[E0463]: can't find crate for `std`
error: cannot find macro `println` in this scope
--> /home/valeriyk/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.70/build.rs:88:9
|
88 | println!("cargo:rustc-cfg=kernel_user_helpers")
| ^^^^^^^
error: cannot find macro `println` in this scope
--> /home/valeriyk/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.70/build.rs:78:9
|
78 | println!("cargo:rustc-cfg=thumb_1")
| ^^^^^^^
...
How come that libcore needs std? I just want to have it cross-compiled using the stage1 rustc, and then picked up during my #![no_std] example compilation. Any guidance is appreciated.
That's how I solved the issue:
Use the right rustc source code. I started with a stable version whose libcore was not compatible with the latest compiler_builtins. See here for more details. Then I needed to upgrade to the latest nightly.
When building the compiler, don't ask to build libcore for the final stage, instead build rustc only:
$ ./x.py build -i --stage=1 --target=arcv2-none-elf32 compiler/rustc
Use the following for the .cargo/config.toml, don't use Xargo or -Z build-std=core. The compiler-builtins-mem thing is explained here.
[unstable]
build-std = [
"core",
"compiler_builtins"
]
build-std-features = ["compiler-builtins-mem"]
[build]
target = "arcv2-none-elf32"
Build the code with a simple cargo build. It compiled just fine, although it failed at linking later - but that's another story.

/usr/bin/link: missing operand after ‘\377\376"’ when compiling bitflags

Following Phillip Opperman's Blog OS, I have been trying to use the bitflags and x86_64 rust crates. The latest x86_64 crate release has bitflags 1.0.4 as a dependency which makes sense. However, I have been completely unable to compile bitflags 1.2.1 on any target with several compilers (tried nightly for every release since June 2019, stable)
Every time I try, Cargo throws this error:
error: linking with `link.exe` failed: exit code: 1
|
= note: "link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.10igbbtzvlu2gjzc.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.178969nxbu3nxuky.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.17xe0ilsif9upn0h.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.18t5fqmvmcakbqcu.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.1tks4n3zsagbiucs.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.1x00x4dovi546kkl.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.20khrbf3w08gf7gw.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.2300zqtgv1madedz.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.242aa09d34jshbc9.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.24klb7r0emqymm3t.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.27en69ffbhnktrj7.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.2bws5627knv64y1n.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.2e1o13g4bta7mxfq.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.2msccpkq1gofr2iq.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.2so84tb4ib2abjhy.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.2wrpuwxc6a2lpmw3.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.34jaofsecblmluop.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3h9hs48cyaiaawqq.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3i7788k802tubyhd.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3jxyc8towsdfxyad.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3ofhy3w7e48bfc7j.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3qnhz4aum3qd07zo.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3y43d86jozz1eymi.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.3ycwew3k0fan7v08.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.40hi0t5daivy49tp.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.447ecvqx3zomb1b8.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.455qjh596c0k58u.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4755rsha0yaachh.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4c1izw4dwgrqtuxe.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4ckqzzupgkkb0m3s.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4e31qbizzcl2oip1.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4l4pis8wno6tow0u.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4p1k3k8ws4isf3lw.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.4tdt9b2iyabiwph8.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.519b3wup2l151966.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.54m1xq349ayksbun.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.57ectnnsg543kzfn.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.58dovvuhpiqcojaw.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.5a7xswmgibmgqieq.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.5du0pnnn1wob2l02.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.5gmrtuynfugoyo5w.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.dy9ewngypkxb53u.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.g92a319cgc2j7m4.rcgu.o"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.o22syf9m375w02v.rcgu.o" "
OUT:C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.exe"
"C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\build\\bitflags-10ba28bcd480cb56\\build_script_build-10ba28bcd480cb56.yhwnnfvr19au4rm.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "
NATVIS:C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "
NATVIS:C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "
NATVIS:C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "
NATVIS:C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:C:\\Users\\jlsat\\Desktop\\projects\\bitflags\\target\\debug\\deps" "
LIBPATH:C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-18c6858731fa3bc3.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-a66e087848bc7936.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-338ca20351402107.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-4f84af8e7d3388ab.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libbacktrace-a1aab3ed9b27c85d.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-7ec4d8ba283cadef.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-95d48056521518ae.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-0da050bb8301bdcc.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-4fecd5ded0f89344.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-6c3a580df1907230.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-a7e28ad09d5bceb4.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-d611b18b12aad85e.rlib"
"C:\\Users\\jlsat\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-72682309e5e16886.rlib" "advapi32.lib" "ws2_32.lib"
"userenv.lib" "msvcrt.lib"
= note: /usr/bin/link: missing operand after ‘\377\376"’
Try '/usr/bin/link --help' for more information.
(More outputs at bottom)
This, to me, is gibberish. Clearly, the linker is failing, but as far as I understand it, the bitflags crate has a custom build script (build.rs) that really only checks for compiler compatibility. Since the crate only works on rustc 1.2+ (something of the sort) it makes sense that it checks this. However, this also leaves me with no leads. What is the source of this issue? I can't see any point where the linker is being directly modified in a way that would cause such a mess.
cargo build --verbose output (same as before with this section appended):
Caused by:
process didn't exit successfully:
`rustc --crate-name build_script_build build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 --cfg 'feature="default"' -C metadata=10ba28bcd480cb56 -C extra-filename=-10ba28bcd480cb56 --out-dir 'C:\Users\jlsat\Desktop\projects\bitflags\target\debug\build\bitflags-10ba28bcd480cb56' -C 'incremental=C:\Users\jlsat\Desktop\projects\bitflags\target\debug\incremental' -L 'dependency=C:\Users\jlsat\Desktop\projects\bitflags\target\debug\deps'` (exit code: 1)
Running this process separately output almost exactly the same thing, with no new information.
Edit:
I was trying this on bash for windows, and my MSVC linker along with the rest of the suite was broken (windows doesn't like me). I believe this massive printout was caused because the terminal defaulted to the gnu linker, which didn't like that. After reinstalling the build tools and updating my toolchain and xbuild, everything worked fine. Thanks for those who tried to help!
I also meet this error.
I fix it by installing Windows 10 SDK.
Rustc need Windows 10 SDK. I found this in here:
For Visual Studio, make sure to check the "C++ tools" and "Windows 10 SDK" option.

Could not able to release a file. getting error: could not compile 'libc'

I am very new to this language and coding field. Beginner to coding field as well.
I tried to build and release file but getting an error Compiling 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)
code: this program which i wrote in VS code to blink LED on raspberry pi 3
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 file:
[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"
I am getting output on Raspberry pi but executable file and binary files are large (5MB). So i thought if i do release maybe i can reduce size so tried to release using cargo build --release command but getting this error.
If you're using rustup-provided binaries, then this is a known issue upstream. There is a workaround in that issue, which is to set the following in Cargo.toml:
[profile.release]
codegen-units = 1
As an alternative, you can use the Debian rustc and cargo packages instead of rustup, which should work just fine. You can either download the packages from https://packages.debian.org/rustc and https://packages.debian.org/cargo, or you can add an appropriate APT line into /etc/sources.list (see https://deb.debian.org/ for an example). Note that Debian does not always have the latest version, but they should work.

Why does clap fail to compile when added to Cargo.toml?

Summary
I'm fairly new to Rust and decided to use it to port an existing project into it. I intended to use clap to handle CLI options, but I keep getting errors.
What do I need to do for clap to install correctly so that it's usable in my project as a dependency (e.g. extern crate clap; [...] use clap::App; [...]?
I haven't had problems with other crates (so far), so I'm not sure what's so different here or if there's a problem with the crate itself.
I've already seen a few questions (e.g. this one), which simply suggests that the dependency be added into the .toml file or don't seem to provide a solution to what I'm seeing.
I'm in Ubuntu Linux, if that makes a difference.
What I Tried
Adding clap = "2.33.0" to my Cargo.toml file (see https://crates.io/crates/clap) causes VSCode (through RLS) to log the following:
{
"resource": "[...]/Projects/takeout/Cargo.toml",
"owner": "rust",
"severity": 8,
"message": "Could not compile `clap`.\nprocess didn't exit successfully: `[...]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rls --crate-name clap [...]/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-2.33.0/src/lib.rs --color never --crate-type lib --emit=dep-info,metadata -C debuginfo=2 --cfg 'feature=\"ansi_term\"' --cfg 'feature=\"atty\"' --cfg 'feature=\"color\"' --cfg 'feature=\"default\"' --cfg 'feature=\"strsim\"' --cfg 'feature=\"suggestions\"' --cfg 'feature=\"vec_map\"' -C metadata=630980a214d5fd10 -C extra-filename=-630980a214d5fd10 --out-dir [...]/Projects/takeout/target/rls/debug/deps -L dependency=[...]/Projects/takeout/target/rls/debug/deps --extern ansi_term=[...]/Projects/takeout/target/rls/debug/deps/libansi_term-1510a9addefc0253.rmeta --extern atty=[...]/Projects/takeout/target/rls/debug/deps/libatty-7c4847fd9fc1e3d9.rmeta --extern bitflags=[...]/Projects/takeout/target/rls/debug/deps/libbitflags-8369a9aec15a5abb.rmeta --extern strsim=[...]/Projects/takeout/target/rls/debug/deps/libstrsim-301d1cf239e9cd24.rmeta --extern textwrap=[...]/Projects/takeout/target/rls/debug/deps/libtextwrap-a799d71e2d028df4.rmeta --extern unicode_width=[...]/Projects/takeout/target/rls/debug/deps/libunicode_width-58e38dd9d658dcfb.rmeta --extern vec_map=[...]/Projects/takeout/target/rls/debug/deps/libvec_map-4f8e59c92e9953d8.rmeta --cap-lints allow --error-format=json --sysroot [...]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu` (exit code: 101)",
"startLineNumber": 1,
"startColumn": 1,
"endLineNumber": 10000,
"endColumn": 1
}
According to the README in the clap repo itself, just adding it should work:
For full usage, add clap as a dependency in your Cargo.toml to use from crates.io:
[dependencies]
clap = "~2.33"
But it doesn't.
I've tried it with and without the ~ prefix as well as clap = {version = "2.33", features = ["yaml"]}, which is also shown in the repo, but no luck. (Yes, I'm trying to load the CLI options from a .yaml file.)
Trying cargo install clap --version 2.33.0 from the shell simply returns an error message saying: error: specified package has no binaries.
Aiming directly at the Git repo also produces the same error message:
cargo install --git https://github.com/clap-rs/clap.git --tag v2.31.2 --features yaml 101 ↵
Updating git repository `https://github.com/clap-rs/clap.git`
Installing clap v2.31.2 (https://github.com/clap-rs/clap.git?tag=v2.31.2#07c15d28)
error: specified package has no binaries
Note that there's no v2.33.0 tag in the Git repo (at the time of this writing).
Bonus if you know how to get VSCode to stop marking everything as an error:
cargo install
There's misunderstanding about the cargo install command. You can learn more about it here.
This command manages Cargo’s local set of installed binary crates. Only packages which have executable [[bin]] or [[example]] targets can be installed, and all executables are installed into the installation root’s bin folder.
It's not your case. The only thing you have to do is to list clap in the dependencies section (Cargo.toml). That's all. No need to use cargo install at all. cargo build, cargo run, ... commands will download & compile & statically link all dependencies.
An example
Folder structure:
.
├── Cargo.toml
└── src
   ├── cli.yaml
   └── main.rs
Current directory:
$ pwd
/Users/robertvojta/Projects/stackoverflow/clap-yaml
Cargo.toml content:
[package]
name = "clap-yaml"
version = "0.1.0"
authors = ["Zrzka"]
edition = "2018"
[dependencies]
clap = { version = "2.33.0", features = ["yaml"] }
src/cli.yaml content:
name: clap-yaml
version: "1.0"
author: Zrzka
about: Stackoverflow sample
args:
- lang:
short: l
long: lang
default_value: cz
takes_value: true
possible_values:
- cz
- en
src/main.rs content:
use clap::{App, load_yaml};
fn main() {
let yaml = load_yaml!("cli.yaml");
let matches = App::from_yaml(yaml).get_matches();
match matches.value_of("lang").unwrap() {
"cz" => println!("Ahoj"),
"en" => println!("Hello"),
_ => unreachable!("see possible_values in yaml, handled by clap"),
};
}
Run it with cargo:
$ cargo -q run -- --lang en
Hello
Run it directly:
$ cargo build
...
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
$ target/debug/clap-yaml --lang cz
Ahoj
Visual Studio Code
I still have vscode complaining and underlining everything in red in the Cargo.toml file. Any suggestions to fix this completely? It seems close to a full resolution.
I can confirm that this problem do exist in Rust 1.34.0. I did install this version and I've got same symptoms:
could not compile clap
the whole Cargo.toml is underlined (error)
There're two ways how to fix this.
Update your Cargo.toml file dependencies section manually if you'd like to stick with Rust 1.34.0:
[dependencies]
bitflags = "=1.0.4"
clap = { version = "2.33.0", features = ["yaml"] }
Or update your Rust toolchain to >= 1.35.0.
I just tested both ways and it works.
Related issues:
Could not compile clap. process didn't exit successfully
Compiler panics on latest RLS when compiling crates that depend on bitflags 1.0.5

failure to build rust-libc using cargo when rustc is musl-enabled

I successfully created a musl configured rustc by following this link
My attempt to build a project (which builds fine using non-musl configured rust) failed when I used cargo rustc -- --target=x86_64-unknown-linux-musl
'error: could not find crate `libc` with expected target triple x86_64-unknown-linux-musl'
Then, I tried to create rust-libc library using the code from crate. To be more accurate, I used the command provided by cargo to build rust-libc, I've only added --target=x86_64-unknown-linux-musl to the command. This time it failed reporting:
'error: could not find native static library `c`, perhaps an -L flag is missing?`'
I have two questions:
Is it mandatory to build musl configured cargo to be able to use cargo build --target=x86_64-unknown-linux-musl?
How can I address this:
'error: could not find native static library `c`, perhaps an -L flag is missing?'
This worked for me to build libc:
rustc --target=x86_64-unknown-linux-musl /address-of-libc/lib.rs --crate-name libc --crate-type lib -L /address-of-musldist/musldist/lib/ --out-dir=/your-chosen-address/target --cfg feature=\"default\" --cfg feature=\"cargo-build\" --emit=dep-info,link

Resources