Cross-compiling for Raspberry Pi: libudev

I’m trying to compile my project which uses tokio-serial library which in turn requires libudev and so far it fails with this error:

  = note: /lib/x86_64-linux-gnu/ file not recognized: file format not recognized                                                                          
          collect2: error: ld returned 1 exit status

These are the steps I performed before building (Ubuntu 18.10):
Add ~/.cargo/config with:

linker = "arm-linux-gnueabihf-gcc"
rustflags = ["-Clink-args=-Xlinker -rpath=/usr/lib/arm-linux-gnueabihf"]

Then do:

sudo apt install build-essential pkg-config libudev-dev gcc-arm-linux-gnueabihf libc6-dev-armhf-cross
rustup target add armv7-unknown-linux-gnueabihf
cargo update
cargo build --target=armv7-unknown-linux-gnueabihf

The full output is here:

Do I need to pass any additional parameters?


1 Like

I also tried following this guide:

which says to do this:



export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig

cargo build

It also doesn’t work:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "`\"pkg-config\" \"--libs\" \"--cflags\" \"libudev\"` did not exit successfully: exit code: 1\n--- stderr\nPackage libudev was not found in the pkg-config search path.\nPerhaps you should add the directory containing `libudev.pc\'\nto the PKG_CONFIG_PATH environment variable\nNo package \'libudev\' found\n"', libcore/
note: Run with `RUST_BACKTRACE=1` for a backtrace.

SYSROOT=/build/root doesn’t seem right, any idea what should be in there?

1 Like

I also tried: with a custom docker image:

FROM japaric/armv7-unknown-linux-gnueabihf:v0.1.9
RUN apt-get update && \
apt-get install -y libudev-dev

It feels like the same way that tokio-serial is using it:

and it seems to work for them.

How do people cross-compile for Raspberry Pi?
Feels like serial port is a pretty common feature, can’t find anything on Google or forums.
I’ve been banging my head against the wall on this for a few days now and I don’t know what to do at this point apart from copying the sources to Raspberry Pi and building it there :frowning:

You mentioned serial ports. I wrote a serial port enumerator (a couple years ago) which doesn’t depend on udev. Instead it uses the /sys filesystem to determine which ports are available. You can find it here:

That’s probably not what you want (now that I read the entire thread) - Sorry for the noise.

Hi @dhylands! Thanks for your response!
It will help me in the future since I can’t use libudev and will probably need to enumerate ports at some point :slight_smile:

I solved my problem by doing the build with

cargo build --target=armv7-unknown-linux-musleabihf

instead of

cargo build --target=armv7-unknown-linux-gnueabihf

this disables libudev dependency in serialport-rs (it’s conditional), so the library doesn’t need to be linked anymore.

I still don’t understand how would I go cross-compiling something with a native library, but it’s a problem to be solved later :slight_smile:

1 Like