Tutorials, Guides, Tips, and Tricks from Everyday Experiences

Archive for the ‘Programming’ Category

The following is a very introductory guide to cross compiling a simple C++ program targeted towards an ARM platform. Development environments differ greatly, but for the most part, they all follow similar steps like outlined below. The very first step is to obtain a cross compile toolchain. CodeSourcery has a free, lite gcc toolchain available for download (as mentioned in a previous post). Once you download an appropriate toolchain for your target, you’ll need to extract it on your development host machine. You’ll notice a directory structure similar to opt/crosstool/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu- which is what we’ll use here.[Read more →]

This is something that I’m still coming to grips with, but today I learned that the cross compile toolchains that Technologic Systems provides for at least some of their products come from CodeSourcery.com. A quick blurb from their website:

CodeSourcery, in partnership with ARM, Ltd., develops improvements to the GNU Toolchain for ARM processors and provides regular, validated releases of the GNU Toolchain. Sourcery G++ Lite Edition supports ARM, Thumb, and Thumb-2 compilation for all architectures in active use, including Version 7 of the ARM Architecture.

A short term goal for myself is to figure out what goes into creating a cross compile environment. I think this would be a valuable skill in the embedded Linux market. For now, this was a shock to learn about how simple it really is to be able to simply download the toolchain and that there wasn’t any other trickery or knowledge required.

A Makefile provides an easy, fast way to compile an application that requires more than just a simple gcc myprogram.c -o myprogram command. For example, to compile a GTK+ app, the common, basic compile command is gcc -g -Wall myguiapp.c -o myguiapp -export-dynamic `pkg-config –cflags –libs gtk+-2.0`. It’s not terrible, but it would be a pain to have to retype that every time you need to recompile. The alternative is to create a Makefile and type make. This is my very first Makefile, and I believe it makes a great example:

As you can see, when make is called, it will compile the source with all my options including the program name. When make clean is called, it will delete the compiled program.

One important thing to remember is that these Makefiles are very picky about spacing and tabs. You MUST use a TAB at the beginning of commands and a TAB must not be at the beginning of blank lines. The first error causes the commands not to run. The second causes the “make” utility to complain that there is a “blank” command.

Say that you have a program that you’d like to be able to compile for both ARM and x86 architectures, but you have portions of the code written in assembly language. Obviously, assembly code written for an ARM CPU won’t compile and run on an x86 CPU, so how would you cleanly solve this problem without writing two different programs? One suggestion would be to use #ifndef (see this page for an explanation) and the ‘-D’ flag when invoking the gcc compiler to tell the compiler to ignore a particular block of code. Here is an example snippet of code that you might want to use in a program:

This program will be developed mostly for the ARM architecture, but we would like to be able to easily compile it for x86 as well. When we do want to compile for x86, we will use the command:

gcc -D X86 myprogram.c -o myprogram_x86

The -D flag is a preprocessor option which, in my understanding, sets a sort of #define for the program before compiling (see ‘man gcc‘ page). The preprocessor will check for any of the #ifndef or #ifdef lines of the program and then act accordingly. In this example, the lines between “#ifndef X86” and “#else” containing the ARM assembly code will be discarded and the lines between “#else” and “#endif” containing the x86 assembly code will be used.