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

<h3>Sysroot</h3>
<p>
The root filesystem of the target system is called <em>sysroot</em>.<br />
It contains header files and precompiled libraries to link against.
</p>
<p>
The official <a href="https://www.raspberrypi.org/downloads/raspbian/" target="_blank">Raspbian image</a>
can be mounted and used as sysroot.</p>
<pre><code class="language-bash">mkdir -p raspberrypi/sysroot
sudo mount_img.sh 2018-04-18-raspbian-stretch.img 2 raspberrypi/sysroot
</code></pre>
<p>
Absolute links need to be fixed to point to files inside sysroot.
</p>
<pre><code class="language-bash">sudo xcompile_fix_links_img.sh raspberrypi/sysroot
</code></pre>
<p>
As an alternative option, the filesystem of a remote target can be mounted over network<br />
using <em>sshfs</em>.
</p>
<pre><code class="language-bash">sshfs pi@raspberrypi:/ raspberrypi/sysroot -o transform_symlinks
</code></pre>

<h3>Toolchain</h3>
<p>The cross-compiling toolchain for the Raspberry Pi is on
<a href="https://github.com/raspberrypi/tools" target="_blank">GitHub</a>.</p>
<pre><code class="language-bash">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"
</code></pre>

<h3>Example</h3>
<p>The following example cross-compiles the base module of
<a href="https://download.qt.io/" target="_blank">Qt</a> for Raspbian.</p>
<pre><code class="language-bash">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
</code></pre>

<h3>External links for further information</h3>
<ul>
<li><a href="https://www.raspberrypi.org/downloads/raspbian/" target="_blank">
https://www.raspberrypi.org/downloads/raspbian/</a></li>
<li><a href="https://github.com/raspberrypi/tools" target="_blank">
https://github.com/raspberrypi/tools</a></li>
</ul>