Stretch armhf cross compile: Difference between revisions

From embeddedTS Manuals
(Created page with "Debian Stretch provides cross compilers from its distribution. An install on a workstation can build for the same release on other architectures. A PC, virtual machine, or ...")
 
(Clarified PC to Linux desktop or laptop.)
Line 1: Line 1:
Debian Stretch provides cross compilers from its distribution.  An install on a workstation can build for the same release on other architectures.  A PC, virtual machine, or chroot will need to be used for this.  Install Debian Stretch for your workstation [https://www.debian.org/releases/stretch/ here].
Debian Stretch provides cross compilers from its distribution.  An install on a workstation can build for the same release on other architectures.  A Linux desktop or laptop PC, virtual machine, or chroot will need to be used for this.  Install Debian Stretch for your workstation [https://www.debian.org/releases/stretch/ here].


From a Debian workstation (not the target), run these commands to set up the cross compiler:
From a Debian workstation (not the target), run these commands to set up the cross compiler:

Revision as of 17:14, 16 March 2018

Debian Stretch provides cross compilers from its distribution. An install on a workstation can build for the same release on other architectures. A Linux desktop or laptop PC, virtual machine, or chroot will need to be used for this. Install Debian Stretch for your workstation here.

From a Debian workstation (not the target), run these commands to set up the cross compiler:

# Run "lsb_release -a" and verify Debian 9.X is returned.  These instructions are not
# expected to work on any other version or distribution.

apt-get install curl build-essential

su root
echo "deb http://emdebian.org/tools/debian stretch main" > /etc/apt/sources.list.d/emdebian.list
curl http://emdebian.org/tools/debian/emdebian-toolchain-archive.key | apt-key add -
# Note that while Ubuntu uses apt as well, Ubuntu host setup is slightly different, instead of the above commands use the following:
# echo "deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports trusty main restricted universe multiverse" >> /etc/apt/sources.list
# echo "deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
# echo "deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
dpkg --add-architecture armhf
apt-get update
apt-get install crossbuild-essential-armhf

This will install a toolchain that can be used with the prefix "arm-linux-gnueabihf-". The standard GCC tools will start with that name, eg "arm-linux-gnueabihf-gcc".

The toolchain can now compile a simple hello world application. Create hello-world.c on the Debian workstation:

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

To compile this:

arm-linux-gnueabihf-gcc hello-world.c -o hello-world
file hello-world

This will return that the binary created is for ARM. Copy this to the target platform to run it there.

Debian Stretch supports multiarch which can install packages designed for other architectures. On workstations this is how 32-bit and 64-bit support is provided. This can also be used to install armhf packages on an x86 based workstation.

This cross compile environment can link to a shared library from the Debian root. The package would be installed in Debian on the workstation to provide headers and .so. This is included in most "-dev" packages. When run on the arm target it will also need a copy of the library installed, but it does not need the -dev package.

apt-get install libcurl4-openssl-dev:armhf

# Download the simple.c example from curl:
wget https://raw.githubusercontent.com/bagder/curl/master/docs/examples/simple.c
# After installing the supporting library, curl will link as compiling on the unit.
arm-linux-gnueabihf-gcc simple.c -o simple -lcurl

Copy the binary to the target platform and run on the target. This can be accomplished with network protocols like NFS, SCP, FTP, etc.

If any created binaries do not rely on hardware support like GPIO or CAN, they can be run using qemu.

# using the hello world example from before:
./hello-world
# Returns Exec format error
apt-get install qemu-user-static
./hello-world