GCC for Embedded Engineers
GCC, the GNU Compiler Collection, is a tool used by nearly every embedded engineer, even those who don't target Linux. In release since 1987, supporting every processor known to man, GCC is a juggernaut of software engineering that, because of its ubiquity and ease of use, doesn't get the admiration it deserves.
When used in an embedded project, GCC capably does another trick, cross-compilation, without complaint. Simply invoke the compiler and the right things will happen. Under the covers, GCC is a very complex tool with lots of knobs to turn to fine-tune the compilation and linking process; this article looks at how to build a GCC cross-compiler, examines the process that GCC uses to compile a program and shares some productivity-boosting tips and tricks.
When starting an embedded project, the first tool needed is a cross-compiler, a compiler that generates code intended to work on a machine different from the one on which the code generation occurred. Sometimes, it's possible to obtain a prebuilt cross-compiler (from a commercial or noncommercial source), short-circuiting the need to build from source; however, some projects require that all tools must be re-creatable from source. No matter why GCC needs to be built, there are several different approaches to building a cross-compiler.
Quite possibly the easiest way is by using the crosstool Project, created by Dan Kegel and hosted at www.kegel.com/crosstool. Using this project involves downloading the source code and making one of the presupplied files feed the right parameters into the script that builds the compiler. The matrix of supported platforms and software versions can be found at www.kegel.com/crosstool/crosstool-0.43/buildlogs, and choosing something that's marked as working will yield a compiler in a few hours. crosstool will download the right software, even the patches, necessary to make the software work on the target platform. However, if the project requires support for an alternate C library, crosstool becomes more difficult to use.
Because many developers want to use uClibc, a smaller implementation of the C library, it's fortunate that this project has something similar to crosstool, called buildroot, located at buildroot.uclibc.net. As a bonus, buildroot, along with building a cross-compiler, also can be used to build a root filesystem for the board based on the related BusyBox Project. The user configures a buildroot run using a process similar to that of the kernel configuration to ready the build. This project doesn't have a chart of known working configurations like crosstool, so finding a working configuration can be difficult.
Finally, for the type of person who doesn't like the idea of wading through somebody else's build scripts when things don't work, building a cross-complier by hand isn't as daunting a process as one would expect. The following steps outline the process, where $TARGET is the target processor and $INSTALLAT is the directory where the compiler will reside after being built:
1. Download and build binutils:
$ tar xzf binutils-<version>.tar.gz $./binutils-<version>/configure --target=$TARGET --prefix=$INSTALLAT $ make ; make install
2. Copy the include and asm from the board's kernel to the installation directory:
$ mkdir $INSTALLAT/include $ cp -rvL $KERNEL/include/linux $KERNEL/include/asm $INSTALLAT/include
3. Download and build bootstrap GCC. At this point, it's best to build the bootstrap GCC in its own directory and not the directory where it has been unpacked:
$ tar xzf gcc-<version>.tar.gz $ mkdir ~/$TARGET-gcc ; cd ~/$TARGET-gcc $../gcc-<version>/configure --target=$TARGET --prefix=$INSTALLAT --with-headers=$INSTALLAT/include --enable-languages="c" -Dinhibit_libc $ make all ; make install
4. Download and build glibc (or alternate libc) with the bootstrap compiler. Like GCC, the build of the library works best when you configure and make outside the source tree:
$ tar xzf glibc-<version> --target=$TARGET --prefix=$INSTALLAT --enable-add-ons --disable-sanity-checks $ CC=$INSTALLAT/bin/$TARGET-gcc make $ CC=$INSTALLAT/bin/$TARGET-gcc make install
5. Build the final GCC. The bootstrap compiler was built to build the C library. Now, GCC can be built to use the cross-compiled C library when building its own programs:
$ cd ~/$TARGET-gcc $../gcc-<version>/configure --target=$TARGET --prefix=$INSTALLAT --with-headers=$INSTALLAT/include --enable-languages="c" $ make all ; make install
At the end of this process, the newly built cross-compiler will be at $INSTALLAT/bin. An oft-used strategy for those needing a specially configured GCC is to use crosstool or buildroot to download and patch the source files and then interrupt the process. At this point, the user applies additional patches and builds the components with the desired configuration settings.
Before leaving this section, there's a frequently asked question from embedded engineers targeting Pentium machines doing development on desktops that are essentially same. In this case, is a cross-compiler necessary? The answer is yes. Building a cross-compiler for this configuration insulates the build environment and library dependencies from the development machine that happened to be used to build the source code. Because desktop systems can change revisions several times a year, and not all team members may be using the same version, having a consistent environment for compiling the embedded project is essential to eliminate the possibility of build configuration-related defects.
- Readers' Choice Awards 2013
- The Geek's Guide to the Coolest 2013 Holiday Gifts
- A Plexible Pi
- Sublime Text: One Editor to Rule Them All?
- RSS Feeds
- Swap Your Laptop for an iPad + Linode
- Raspberry Pi: the Perfect Home Server
- Linux Kernel News - November 2013
- Mars Needs Women
- Tech Tip: Really Simple HTTP Server with Python
- Planet Security
34 min 34 sec ago
- "RasPlex Linux-based OS
8 hours 13 min ago
- Clarification; RasPlex is not
15 hours 2 min ago
- Clarification; RasPlex is not
15 hours 4 min ago
- Starting the conversation is the first step.
16 hours 4 min ago
17 hours 51 min ago
17 hours 51 min ago
- Nice but....
18 hours 13 min ago
- great specs
1 day 43 min ago
- Reply to comment | Linux Journal
1 day 1 hour ago