Cross compilation creates executable binary code for a target architecture that differs
from the host architecture. For example, the compiler runs on x86_64 (common desktop PC)
and produces output for armv7l (Raspberry Pi).

Sysroot

The root filesystem of the target system is called sysroot.
It contains header files and precompiled libraries to link against.

The official Raspbian image can be mounted and used as sysroot.

mkdir -p raspberrypi/sysroot
sudo mount_img.sh 2018-04-18-raspbian-stretch.img 2 raspberrypi/sysroot

Absolute links need to be fixed to point to files inside sysroot.

sudo xcompile_fix_links_img.sh raspberrypi/sysroot

As an alternative option, the filesystem of a remote target can be mounted over network
using sshfs.

sshfs pi@raspberrypi:/ raspberrypi/sysroot -o transform_symlinks

Toolchain

The cross-compiling toolchain for the Raspberry Pi is on GitHub.

mkdir raspberrypi
cd raspberrypi
git clone https://github.com/raspberrypi/tools
export PATH="$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin"

Example

The following example cross-compiles the base module of Qt for Raspbian.

cd qtbase
./configure -opensource -confirm-license \
  -device linux-rasp-pi2-g++ \
  -device-option CROSS_COMPILE=arm-linux-gnueabihf- \
  -sysroot "$HOME/raspberrypi/sysroot"
make
make install   # installs to the given sysroot

External links for further information