Difference between revisions of "EBC Exercise 08a Cross-Compiling"

From eLinux.org
Jump to: navigation, search
m (Cross-compiling Hello World: Added Bitbake to crossCompileEnv.sh)
(Cross-compiling Hello World: Fixed for apt-get loaded cross-compiler)
Line 6: Line 6:
  
 
== Cross-compiling Hello World ==
 
== Cross-compiling Hello World ==
 +
This shows how to cross-compile with compiler loaded with
 +
 +
host$ '''sudo apt-get install -y git lzop gcc-arm-linux-gnueabi uboot-mkimage'''
  
 
Listing 2-4 on page 29 of the text is an embedded version of Hello World. If you've set up your git repository you will find it in '''helloWorld.c''' when you do a''' git pull'''.  Compile and run it on your host to be sure it works.
 
Listing 2-4 on page 29 of the text is an embedded version of Hello World. If you've set up your git repository you will find it in '''helloWorld.c''' when you do a''' git pull'''.  Compile and run it on your host to be sure it works.
Line 16: Line 19:
 
  This address (0x601020) is in our data section
 
  This address (0x601020) is in our data section
  
Now that you know it's working, let's cross compile it. First set the paths to find the cross-compilers. Put the following in a file, call it ~/.oe/'''crossCompileEnvBitbake.sh'''. Make sure the path is correct for your system. This is for a 32-bit linux.  
+
Now that you know it's working, let's cross compile it. First set the paths to find the cross-compilers. Put the following in a file, call it ~/.oe/'''crossCompile.sh'''. Make sure the path is correct for your system. This is for a 32-bit linux.  
 
+
# add cross tools to your path
+
export ARM_TOOLCHAIN_PATH=~/BeagleBoard/oe/build/tmp-angstrom_v2012_05-eglibc/sysroots/i686-linux/usr/bin/armv7a-angstrom-linux-gnueabi
+
PATH=$PATH:$ARM_TOOLCHAIN_PATH/..
+
PATH=$ARM_TOOLCHAIN_PATH:$PATH 
+
export ARCH=arm
+
export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
+
 
+
Here it is for 64-bit
+
  
  # add cross tools to your path
+
  # add cross tools to your path  
export ARM_TOOLCHAIN_PATH=~/BeagleBoard/oe/build/tmp-angstrom_v2012_05-eglibc/sysroots/x86_64-linux/usr/bin/armv7a-angstrom-linux-gnueabi
+
PATH=$PATH:$ARM_TOOLCHAIN_PATH/..
+
PATH=$ARM_TOOLCHAIN_PATH:$PATH 
+
 
  export ARCH=arm
 
  export ARCH=arm
  export CROSS_COMPILE=arm-angstrom-linux-gnueabi-
+
  export CROSS_COMPILE=arm-linux-gnueabi-
  
 
Now ''source'' the file and compile again. (Note: you only have to source once per terminal session.)
 
Now ''source'' the file and compile again. (Note: you only have to source once per terminal session.)
  host$ '''source ~/.oe/crossCompileEnvBitbake.sh'''
+
  host$ '''source ~/.oe/crossCompileEnv.sh'''
 
  host$ '''${CROSS_COMPILE}gcc helloWorld.c'''
 
  host$ '''${CROSS_COMPILE}gcc helloWorld.c'''
 
  host$ '''file a.out'''
 
  host$ '''file a.out'''

Revision as of 18:26, 29 October 2012

thumb‎ Embedded Linux Class by Mark A. Yoder


This class is about developing software for embedded Linux. So far we have been doing all of our development on the Beagle. This works well for small (and not so small) programs. However, we are now moving into kernel development and that's best done on a more powerful host computer. In EBC Exercise 08 Installing Development Tools you learned how to download and install the cross-compilers and the source for kernel and u-boot. Now we'll use those tools.

Cross-compiling Hello World

This shows how to cross-compile with compiler loaded with

host$ sudo apt-get install -y git lzop gcc-arm-linux-gnueabi uboot-mkimage

Listing 2-4 on page 29 of the text is an embedded version of Hello World. If you've set up your git repository you will find it in helloWorld.c when you do a git pull. Compile and run it on your host to be sure it works.

host$ gcc helloWorld.c
host$ ./a.out
Hello, World! Main is executing at 0x400524
This address (0x7fff8260bdf8) is in our stack frame
This address (0x601038) is in our bss section
This address (0x601020) is in our data section

Now that you know it's working, let's cross compile it. First set the paths to find the cross-compilers. Put the following in a file, call it ~/.oe/crossCompile.sh. Make sure the path is correct for your system. This is for a 32-bit linux.

# add cross tools to your path 
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-

Now source the file and compile again. (Note: you only have to source once per terminal session.)

host$ source ~/.oe/crossCompileEnv.sh
host$ ${CROSS_COMPILE}gcc helloWorld.c
host$ file a.out
a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped

The file command tells what's in the file. In this case we have an ARM executable. Success! Now copy to your Beagle and run

host$ scp a.out root@beagle:.
host$ ssh root@beagle ./a.out
Hello, World! Main is executing at 0x8374
This address (0xbeb32d4c) is in our stack frame
This address (0x10650) is in our bss section
This address (0x10648) is in our data section

The scp copies a.out to the beagle and the ssh runs the a.out on the beagle. Notice the address are very different from the host version.




thumb‎ Embedded Linux Class by Mark A. Yoder