Embedded computers frequently lack the necessary resources for developing and compiling software. The Raspberry Pi is rather special in this regard since it already includes the gcc compiler and the needed linking tools (under Raspbian Linux). But while the code can be developed and built on the Raspberry Pi, it may not always be the most suitable place for software development. One reason is the lower performance of the SD card, while the Pi Zero or Zero W may be underperforming in this regard.
To compile native code for the Raspberry Pi, you need a compiler and linker that knows how to generate ARM binary executables. Yet it must run on a host with a different architecture (for example, Mac OS X). Hence the reason it is called a cross-compiler. The cross-compiler will take your source code on the local (build) platform and generate ARM binary executables, to be installed on your target Pi.
In this chapter, you’ll walk through how to build your own cross-compiler. This will permit you to get the job done using your existing Linux platform.
Terminology
build: Also called the local platform, this is the platform that you perform the compiling on (for example, Mac OS X).
target: The destination platform, which is the Raspberry Pi (ARM) for this chapter.
All C/C++ include files and libraries for the Raspberry Pi (ARM) must be available on your build platform (when building the kernel, for example).
The cross-compiler and related tools must generate code suitable for the target platform.
Provide all matching C/C++ header files from the ARM platform?
Provide all ARM libraries needed, including libraries for third-party products like sqlite3 that you intend to link with?
Provide sufficient disk space for the cross-compiler and tools?
The crosstool-NG software will mitigate some of these issues. For example, the correct Linux headers are chosen by the configuration steps shown later in this chapter.
Disk space solves many issues by holding a copy of your Raspberry Pi’s root file system on your build platform. Simple programs won’t require this (for example, a Hello World program). But software linking to libraries may require this. Even if you’re strapped for disk space, you may be able to mount the Raspbian SD card on the build platform, thus gaining access to the Raspberry Pi’s root file system.
Operating System
The procedure used for building a cross-compiler environment is somewhat complex and fragile. Using the crosstool-NG software simplifies things considerably. Despite this advantage, it is best to stick with proven cross-compiler platforms and configurations.
You might be tempted to say, “The source code is open, and so it should work on just about any operating system.” (You might even say, “I’ll fix the problems myself.”) The reality is not quite so simple. Unless you are willing to spend time on Internet forums and wait for answers, I suggest you take a more pragmatic approach—build your cross-compiling environment on a recent and stable Ubuntu or Debian/Devuan environment.
This chapter uses a recent install of Devuan, which is Debian based on an older 32-bit computer donated to the lab here. You can use VirtualBox 4.3.12 ( www.virtualbox.org ) on a Mac OS X MacBook Pro, running an Intel i7 processor instead if you like. Current versions of Debian or Devuan Linux are recommended.
Host, Guest, Build, and Target
Host environment
Guest environment
Build/local environment
Target environment
So many environments! The terms host and guest environments enter the picture when you are using a virtual machine like VirtualBox. VirtualBox is used to host another operating system on top of the one you are using. For example, you might be running Mac OS X on your laptop. In this example, the OS X environment hosts Linux instance within VirtualBox. That Linux operating system is thus referred to as the guest operating system.
The term build (or local) environment refers to the Linux environment that is executing the cross-compiler and tools. These Linux tools produce or manipulate code for the target environment (the Raspberry Pi’s ARM CPU).
Platform Limitations
Many people today are using 64-bit platforms like the MacBook Pro, with an Intel i7 processor or similar. This may present a problem if you want to build a cross-compiler for the Raspberry Pi, which is a 32-bit platform. The 32-bit cross-compiler must be built on a 32-bit processor.
Re: Can we get an 64bit OS?
Sat Dec 23, 2017 3:30 pm
AFAIK no there isn't. They don't want to split the OS so they have to support two different OS's, one for PI3 only and another one for all older PI's, and get lots of confused users that complain the 64-bit OS won't work on their older PI.
Also switching to 64-bit CPU's brings almost nothing, not much more speed for example.
If you are using a 64-bit platform, then you’ll probably want to choose a VirtualBox solution or use an older Linux 32-bit server. This gives you a 32-bit operating system to host the cross-compiler on. On the other hand, if you are already running a 32-bit operating system, creating a native cross-compiler should be a slam dunk.
Note
You will need to host the cross-compiler on a 32-bit platform. The cross-compiler does not build on 64-bit platforms.
Without VirtualBox (Native)
Host/guest/build: Native environment running the cross-compiler tools
Target: The destination execution environment (Raspberry Pi)
Using VirtualBox (Debian/Linux)
If you do not have a suitable Linux environment, one can be hosted on the platform you have. You can host Linux from Windows, Mac OS X, Solaris, or another distribution of Linux using VirtualBox downloaded from the following:
Host: Or native, running VirtualBox (for example, Windows)
Guest/build: Debian/Ubuntu development environment within VirtualBox
Target: The destination execution environment (your Raspberry Pi)
Planning Your Cross-Development Environment
In the preceding example output, we see that the root file system has plenty of space. But your file system is likely to be different. Symlinks can be used when necessary to graft a larger disk area onto your home directory.
If you’re using VirtualBox, create virtual disks with enough space for the Linux operating system and your cross-compiler environment. You may want to put your Linux software on one virtual disk with a minimum size of about 10 GB (allow it to grow larger).
Allow a minimum of 10 GB for your cross-compiler environment (and allow it to grow). You must also factor in additional space for the Raspberry Linux kernel, its include files, and all other third-party libraries that you might need to build with (better still, a copy of the Raspberry Pi’s root file system).
Within your development Linux build environment, make sure your cross-compiler and build area are using the disk area that has available space. It is easy to glibly create a directory someplace convenient and find out later that the space that you thought you were going to use wasn’t available.
Building the Cross-Compiler
At this point, I’ll assume that you’ve set up and installed Linux in VirtualBox, if necessary or using an instance of 32-bit Linux. I'll be using a Devuan Linux, which is based upon Debian.
Download crosstool-NG
The released crosstool-NG downloads are found at:
Staging Directory
Unpack the Tarball
After the unpacking completes, you should have a subdirectory named crosstoolng-1.23.0 in your staging directory.
Create /opt/x-tools
Optionally, once the installation is complete later, you can change the ownership back to root for protection.
Install Package Dependencies
If during the configuration of crosstools-ng you find that you need other packages, they can be installed then and the configuration retried.
Configure crosstools-NG
If this completes without errors, you are ready to build and install the software. If it reports that you are missing package dependencies, install them now and repeat.
Patch inputbox.c
One extremely vexing irritation I have with some modern Linux and tools, is that they don't properly support the backspace character (Control-H). This is a standard ASCII character intended precisely for this purpose. Why has it been banished? I'll try to refrain from further ranting about this.
The menu program used by the crosstool-ng suffers from the same problem that the linux kernel menuconfig suffers from: no backspace character (Control-H) support. This can leave you in the curse-worthy situation of not being able to backspace or delete input.
- 1.
cd ~/xrpi/devel/staging/crosstool-ng-1.23.0/kconfig/lxdialog
- 2.Edit file inputbox.c and go to about line 128, where you should see a line that says:case KEY_BACKSPACE:
- 3.Add a line below it that simply reads:case 8:
- 4.
Save the file (inputbox.c).
If you already know that your backspace key sends an escape sequence instead of Control-H, then you can safely skip this change. Otherwise, this kind of thing can drive you absolutely batty. With that file saved, some semblance of sanity will follow.
make crosstool-ng
This takes very little time and seems trouble free.
make install
If you still own the directory /opt/x-tools from earlier (recall sudo chown fred /opt/x-tools), you won’t need to use sudo in the preceding step. After make install is performed, you will have the crosstool-NG command ct-ng installed in the directory /opt/x-tools/bin.
PATH
Cross-Compiler Configuration
The command ct-ng simplifies the work necessary to configure and build the cross-compiler tool chain. From here, we are concerned with building the cross-compiler tools themselves. When that process is completed, you will have populated the cross-compiler tools into the directory /opt/x-tools/arm-unknown-linux-gnueabi.
If you get a “command not found” error message, check that the PATH variable is set correctly.
Paths and Misc Options
Once the pathname is established, press Enter on the OK button shown. This returns you to the Paths and Misc Options menu.
Target Options
Finally, exit this submenu with the Exit button.
Operating System
Once you have chosen “linux” in Figure 20-12, exit back to the main menu.
Binary Utilities
Exit back to the main menu.
C Compiler
Then choose Exit once again to return to the main menu.
Save Configuration
Unless you have a reason to change anything else, exit the menu again to cause the Save prompt to appear:
Saving the file outside the current directory will prevent accidental loss if ct-ng distclean is used.
Build Cross-Compiler
Based upon the 222:33 figure reported, this download and build took approximately 4 hours and 15 minutes to complete. This build was performed on an older single processor Devuan Linux instance.
Troubleshooting
Using less, you can navigate to the end of the build.log file by typing a capital G.
One failure that frequently occurs in the beginning is a failed download. While the build process does retry downloads and tries different download methods, it can still fail. All that you need to do is to retry the build. It will download only the remaining files needed. Sometimes it will succeed on the second or third retry attempt.
Navigate to the end of config.log and work backward a few pages. Eventually, you will see text describing the command that was tried and the error message produced. In one instance, I was able to determine that the custom compiler option that I added (-fpermissive) was causing the failure. The solution then was to remove that option and try again.
Some errors will only occur with certain version choices. At one time, I was receiving errors related to PPL and needed a patch to correct it.
In getting through these issues, you can simply make corrections and then rerun the ct-ng build command. It is recommended that you plan for a later rebuild of everything again (after a clean), once the problems are solved. This will ensure that you have a good build without dependency issues.
ct-ng clean
ct-ng distclean (Be careful; see the following text.)
The ct-ng clean command will usually be enough, forcing the next build to start fresh. Any downloaded files and configuration will remain and will be reused.
The ct-ng distclean command is much more drastic, since it removes all of the downloaded content and your configuration files. I had copied the .config file to .config.bak and discovered to my horror that .config.bak had also been removed! So if you back up the .config file, copy it outside the current directory for safety.
Above all, keep your head. It’s difficult to troubleshoot these issues if you feel time pressure or get angry over the time invested. When under time pressure, leave it for another day when you can deal with it thoughtfully. Each redo takes considerable time. Wherever possible, eliminate the guesswork.
With each problem, take a deep breath, patiently look for clues, and pay attention to the details in the error messages. Remember that line in the movie Apollo 13: “Work the problem, people!”
Summary
In this chapter you have seen how to install a cross-compiler for your Raspberry Pi, whether on an older 32-bit Linux platform or in an instance of VirtualBox. Doing this will provide you with the compiler tools needed to compile your kernel or applications for the lesser powered Raspberry Pis, like the Zero or Zero W.