Cross-compiling for Raspberry Pi: libudev


#1

Hi!
I’m trying to compile my project https://github.com/Leonti/rust-socket-server which uses tokio-serial library which in turn requires libudev and so far it fails with this error:

  = note: /lib/x86_64-linux-gnu/libudev.so: 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:

[target.armv7-unknown-linux-gnueabihf]
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
export PKG_CONFIG_ALLOW_CROSS=1
cargo update
cargo build --target=armv7-unknown-linux-gnueabihf

The full output is here: https://pastebin.com/B2ERXa6y

Do I need to pass any additional parameters?

Cheers,
Leonti


#2

I also tried following this guide:


which says to do this:

#!/bin/sh

SYSROOT=/build/root

export PKG_CONFIG_DIR=
export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=${SYSROOT}
export PKG_CONFIG_ALLOW_CROSS=1

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/result.rs:1009:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

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


#3

I also tried: https://github.com/rust-embedded/cross with a custom docker image:

FROM japaric/armv7-unknown-linux-gnueabihf:v0.1.9
ENV PKG_CONFIG_ALLOW_CROSS=1
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:


#4

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: https://github.com/dhylands/serial-ports-rs

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


#5

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: