4
Mar

Simple Cross Compile Example

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.

We must make a few other assumptions before beginning: 1.) The cross compiler will be located at ~/crosstool (~ is equivalent to $HOME) and 2.) The program will be located at ~/main.cpp which is also your current working directory (pwd).

01.) Use the following greatest common divisor (gcd.cpp) file as an example if
needed:

#include
using namespace std;

int GCD(int a, int b)
{
   while( 1 )
   {
      a = a % b;
      if( a == 0 )
      return b;
      b = b % a;

      if( b == 0 )
      return a;
   }
}

int main()
{
   int x, y;

   cout << "This program allows calculating the GCDn";
   cout << "Value 1: "; 
   cin >> x;
   cout << "Value 2: ";
   cin >> y;

   cout << "nThe Greatest Common Divisor of "
   << x << " and " << y << " is " << GCD(x, y) << endl;

   return 0;
}

02.) Use the cross compiling toolchain to compile the program:

./crosstool/opt/crosstool/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-g++ gcd.cpp -o gcd

03.) Copy the program to your target platform and then test by running it (alternatively, you could run it from an NFS server). You will see:

ts7500:~# ./gcd
This program allows calculating the GCD
Value 1: 8
Value 2: 2

The Greatest Common Divisor of 8 and 2 is 2
ts7500:~#

04.) Additionally, you can include the crosstools in your PATH for less typing:

PATH=$HOME/crosstool/opt/crosstool/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/:$PATH
export PATH
arm-linux-uclibc-g++ gcd.cpp -o gcd

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 2 Comments So Far

  • Shashikiran
    March 8th, 2011 at 9:41 pm

    Need clear steps to cross compile simple kernel modules

  • Dasari Sai Mounika
    July 14th, 2011 at 2:26 am

    provide complete information of types&examples of cross compilers

Share your thoughts, leave a comment!