Cross-compiling for SBCs

The compile process takes the source files, turning them into an intermediate format, after which this format can be used to target a specific CPU architecture. For us, this means that we aren't limited to compiling applications for an SBC on that SBC itself, but we can do so on our development PC.

To do so for an SBC such as the Raspberry Pi (Broadcom Cortex-A-based ARM SoCs), we need to install the arm-linux-gnueabihf toolchain, which targets the ARM architecture with hard float (hardware floating point) support, outputting Linux-compatible binaries.

On a Debian-based Linux system, we can install the entire toolchain with the following commands:

sudo apt install build-essential
sudo apt install g++-arm-linux-gnueabihf
sudo apt install gdb-multiarch  

The first command installs the native GCC-based toolchain for the system (if it wasn't already installed), along with any common related tools and utilities, including make, libtool, flex, and others. The second command installs the actual cross-compiler. Finally, the third package is the version of the GDB debugger that supports multiple architectures, which we'll need later on for doing remote debugging on the real hardware, as well as for analyzing core dumps produced when our application crashes.

We can now use the g++ compiler for the target SBC using its full name on the command line:

arm-linux-gnueabihf-g++  

To test whether the toolchain was properly installed, we can execute the following command, which should tell us the compiler details including the version:

arm-linux-gnueabihf-g++ -v  

In addition to this, we may need to link with some shared libraries that exist on the target system. For this, we can copy the entire contents of the /lib and /usr folders and include them as part of the system root for the compiler:

mkdir ~/raspberry/sysroot
scp -r pi@Pi-system:/lib ~/raspberry/sysroot
scp -r pi@Pi-system:/usr ~/raspberry/sysroot  

Here, Pi-system is the IP address or network name of the Raspberry Pi or similar system. After this, we can tell GCC to use these folders instead of the standard paths using the sysroot flag:

--sysroot=dir  

Here dir would be the folder where we copied these folders to, in this example that would be ~/raspberry/sysroot.

Alternatively, we can just copy the header and library files we require and add them as part of the source tree. Whichever approach is the easiest mostly depends on the dependencies of the project in question.

For the club status service project, we require at the very least the headers and libraries for WiringPi, as well as those for the POCO project and its dependencies. We could determine the dependencies we need and copy the required includes and library files that are missing from the toolchain we installed earlier. Unless there's a pressing need to do so, it's far easier to just copy the entire folders from the SBC's OS.

As an alternative to using the sysroot method, we can also explicitly define the paths to the shared libraries that we wish to use while linking our code. This of course comes with its own set of advantages and disadvantages.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.15.94