Cross-compiling a Sample Application Using Automotive Grade Linux

Paulo Sherring
Nerd For Tech
Published in
6 min readFeb 24, 2021

--

The purpose of this article is to describe the process I use to cross-compile and deploy a simple program to a Raspberry Pi 4 target, under the umbrella of Automotive Grade Linux (AGL). You will get a step-by-step to generate a more development grade ecosystem, use it to cross-compile a sample application, deploy it to your Raspberry Pi 4 target running the image you will build, and run it directly from you target, through a ssh remote shell.

Cross-compiling, on its own, is not a trivial task to new-comers, so, if anything doesn’t make sense to you, please, drop me a note and/or read through on other sources on cross-compilation.

Getting the development tools

So, if you followed through Running Automotive Grade Linux on a Raspberry Pi 4, I have somewhat bad news: you will need to change a few things, here and there, to get the correct tooling. That is because the default configuration does not include a few essential tools, most notably GDB. Also, in that previous article, a Software Development Kit (SDK) was not generated.

I will assume you have your Host system setup. If you don’t, please either install Ubuntu 16.04 or use the docker image we’ve configured on Running Automotive Grade Linux on a Raspberry.

The first change is to add a few lines to build/conf/local.conf in order to add the desired content to the flashable image as well as to the installable SDK. Here, around line 143, I read EXTRA_IMAGE_FEATURES ?= "debug-tweaks". Below that, add the following:

EXTRA_IMAGE_FEATURES += "tools-sdk"
EXTRA_IMAGE_FEATURES += "tools-debug"
EXTRA_IMAGE_FEATURES += "eclipse-debug"

The description for these are immediately above the insertion point on the build/conf/local.conf, but I copy here anyways:

  • “tools-sdk” — add development tools (gcc, make, pkgconfig etc.);
  • “tools-debug” — add debugging tools (gdb, strace);
  • “eclipse-debug” — add Eclipse remote debugging support.

After these changes, we are ready to bitbake. The following command lines assume you are using the docker image for that, so you may have to adapt the path to the repo tool.

./../bin/repo init -b koi -u https://gerrit.automotivelinux.org/gerrit/AGL/AGL-repo
./../bin/repo sync
source meta-agl/scripts/aglsetup.sh -m raspberrypi4 agl-demo agl-appfw-smack
bitbake agl-demo-platform && bitbake agl-demo-platform-crosssdk

Note that the last line will build for two targets: agl-demo-platform and agl-demo-platform-crosssdk. The former will generate the target's image, the latter will generate both a target's image and an installable SDK. If this is your first build, it should take a few hours to complete.

The resulting image and SDK installer can be found on /build/tmp/deploy/images/raspberrypi-64 and /build/tmp/deploy/sdk respectively.

Flash a sdcard for your newly generated image. From a terminal, navigate to ~/agl/out/build/tmp/deploy/images/raspberrypi4–64. There, you will find a flashable image, a wic file compressed with xz. To flash your new image, run:

xzcat {yourimagename}.wic.xz| sudo dd of=/dev/{your-sd-disk} bs=4M status=progress
sync

Install the SDK. From a terminal, navigate to ~/agl/out/build/tmp/deploy/sdk and run your SDK installer file:

./poky-agl-glibc-x86_64-agl-demo-platform-crosssdk-aarch64-raspberrypi4-64-toolchain-10.93.1.sh

You will have to interact with the terminal:

  • You will be asked to input destination path to hold your SDK, with the option to use a default path, /opt/agl-sdk/10.93.1-aarch64;
  • You will be asked if you want to follow through with the installation: y;
  • You will be asked admin credentials: enter your password.

The installer will start. It should take a while to complete. Upon completion, you get a pretty basic and yet nice instruction on usage:

Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
$ . /opt/agl-sdk2/10.93-1-aarch64/environment-setup-aarch64-agl-linux

Building a Sample Application

To build your first application, first, initialize your SDK. Open a terminal and source the initialization script: . /opt/agl-sdk/10.93-1-aarch64/environment-setup-aarch64-agl-linux. After doing that, you should get a few environment variables initialized. You can check them with export.

At this point, you can reach GCC's cross-compiler by entering the $CC environment variable. For instance, you can check the cross-compiler's version by entering $CC --version:

If you use an IDE, you need to source this file prior to starting the IDE itself. For instance, having sourced it, you can run Microsoft’s Visual Studio Code by running it from the same terminal session you sourced the file, by running code .

If you enjoy using VSCode, please, do open it and run it. As a tip, to run commands from inside VSCode, press F1. With that said, do the following:

  • Open an empty folder as a workspace;
  • Run CMake’s Extension Cmake: Scan For Kits;
  • Run CMake’s Extension Cmake: Quick Start to generate a new template project;
  • Select your cross-compiler from the list;
  • You may be asked if you want to create a CMakeLists.txt file. Hit create if you do;
  • Enter the Sample application’s name: SampleApp ;
  • Enter the output type: executable ;
  • Run CMake’s Extension Cmake: Build .
Using CMake’s extension from VSCode.

Presto! You have built your cross-compiled Hello World, generated to ./build/SampleApp (or whatever name you gave the project :)

Running the application on the target

Now you have a target with the appropriate image and a binary compatible with it. You just need to transfer it to the target and run it! I will assume here that you have access to your target’s shell over ssh and that you know its IP address.

The steps required are the following:

  • Deploy the resulting binary the target;
  • Access your target over SSH;
  • Run your application from SSH.

Bear in mind that you may need to adapt your target’s IP as necessary.

scp ./SampleApp root@192.168.5.2:/home/1001

Deploy binary: scp ./SampleApp root@192.168.5.2:/home/1001

SSH into your target: ssh -l root 192.168.5.2

Navigate to /home/1001: cd /home/1001

Run your sample application: ./SampleApp

And you should get your embedded Hello World.

Deploying and running your Linux embedded, cross-compiled, Hello World.

Conclusion

In this article, you have used AGL to generate a more development grade ecosystem, developed a Hello World using VSCode, CMake, and the AGL’s SDK that you build by yourself! You have also deployed and run it on your Raspberry Pi 4 target.

I hope it helps you :)

If so, please let me know. Also, feedbacks will be much appreciated, as this is my second post.

I have plans to write yet another follow-up article on remote debugging using GDB, both from an IDE and from the command line.

--

--

Paulo Sherring
Nerd For Tech

Passionate about electronics, technology and science. Electrical Engineer scholar. Embedded Systems Software Developer.