Buster armhf cross compile

From embeddedTS Manuals

Debian only provides their cross compiler for their distribution. Our examples will set up a chroot for Debian to use for development. If using Debian 10 Buster directly, or through a VM, the schroot commands and debootstrap can be skipped.

First install the host system dependencies to use schroot and debootstrap:

# Ubuntu/Debian
sudo apt-get install debootstrap schroot
# Fedora
sudo dnf install debootstrap schroot
# Centos/redhat
sudo yum install debootstrap schroot

Use debootstrap to install a base Debian 10 for your host.

# Generate Debian Buster rootfs
sudo debootstrap buster /opt/chroots/buster-armdev/ http://deb.debian.org/debian

Then configure schroot to enter this rootfs. Replace "youruser" with your linux username.

sudo tee /etc/schroot/chroot.d/buster-armdev <<'EOF' >/dev/null
[buster-armdev]
description=Debian Buster for ARM development
directory=/opt/chroots/buster-armdev/
root-users=youruser
users=youruser
type=directory
EOF

Log into this schroot and install the armhf development tools (Skip if running a native Debian Buster host):

schroot -c buster-armdev

This will change your PS1 variable to indicate that you are in the Debian root. For example:

mark@mark-desktop:~$ sudo schroot -c buster-armdev
(buster-armdev)root@mark-desktop:/home/mark#

Install Debian's development tools for armhf:

# This workaround is required for Debian Buster
rm /var/lib/dpkg/statoverride

dpkg --add-architecture armhf
apt-get update
apt-get install -y build-essential gcc-arm-linux-gnueabihf bc \
  lzop u-boot-tools libncursesw5-dev file wget 
exit

At this point the Debian chroot is ready to compile armhf binaries. For example, create a hello world in your home folder at ~/Documents/hello.c

#include <stdio.h>
int main(){
    printf("Hello World\n");
}

To compile this, you can enter the schroot as a normal user:

schroot -c buster-armdev

Keep in mind that when this is run as a normal user it does not modify your prompt. This will look like any other prompt, but will use your Debian applications instead. You can verify this is Debian with:

mark@mark-desktop:~/Documents$ cat /etc/issue
Debian GNU/Linux 10 \n \l

While logged into this schroot, run:

arm-linux-gnueabihf-gcc hello.c -o hello
exit

From here you can check "file hello" to verify the binary type:

mark@mark-desktop:~/Documents$ file hello
hello: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=8a8cee3341d3ef76ef6796f72d5722ae9d77c8ea, not stripped

This can also be used to develop against dynamic libraries from Debian. The armhf packages can be installed in the chroot. For example, to link against curl:

# Run as root to install dependencies
sudo schroot -c buster-armdev
apt-get install libcurl4-openssl-dev:armhf
exit
# Return to arm chroot as a normal user:
schroot -c buster-armdev
# Download curl's simple.c example
wget https://raw.githubusercontent.com/bagder/curl/master/docs/examples/simple.c
arm-linux-gnueabihf-gcc simple.c -o simple -lcurl

The "simple" binary is now built for armhf and links dynamically to curl.