Difference between revisions of "EBC Exercise 08a Cross-Compiling"
m (moved EBC Exercise 22 Cross-Compiling and Finding the Right Kernel to EBC Exercise 22 Cross-Compiling: Kernel and U-boot is now in Exercise 08) |
m (→Cross compiling Hello World: Updated for new compiler version. Switched from root to debian) |
||
(9 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
{{YoderHead}} | {{YoderHead}} | ||
− | 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 [[ | + | 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_4.4]] 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 | + | == Cross compiling Hello World == |
+ | Normally when you compile you compile on the machine that will run the code. You can compile and run on the Bone, but sometimes (like when compiling the kernel) it's better to use a more powerful machine for the compiling. First we'll compile ''helloWorld.c'' on the host computer and run it there, then we'll cross compile it on the host to run on the Bone. | ||
− | + | 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$ '''gcc helloWorld.c''' | ||
+ | host$ '''file a.out''' | ||
+ | a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), | ||
+ | dynamically linked (uses shared libs), for GNU/Linux 2.6.24, | ||
+ | BuildID[sha1]=0x357e34e90f7c32d414368d69cc06d0aed59acf1c, not stripped | ||
host$ '''./a.out''' | host$ '''./a.out''' | ||
Hello, World! Main is executing at 0x400524 | Hello, World! Main is executing at 0x400524 | ||
Line 16: | Line 21: | ||
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 | + | Now that you know it's working, let's cross compile it. First figure out what version of the cross compiler was loaded. |
− | + | host$ '''cd BeagleBoard/bb-kernel/dl''' | |
− | + | host$ '''ls''' | |
− | + | gcc-linaro-5.4.1-2017.05-x86_64_arm-linux-gnueabihf | |
− | + | gcc-linaro-5.4.1-2017.05-x86_64_arm-linux-gnueabihf.tar.xz | |
− | + | gcc-linaro-7.1.1-2017.08-x86_64_arm-linux-gnueabihf | |
− | + | gcc-linaro-7.1.1-2017.08-x86_64_arm-linux-gnueabihf.tar.xz | |
+ | |||
+ | Here we see two versions of the compiler have been loaded. Let's pick the newer one. | ||
− | + | Now set the paths to find the cross-compiler. Put the following in a file, call it '''~/crossCompileEnv.sh'''. | |
− | |||
− | |||
− | |||
− | |||
export ARCH=arm | export ARCH=arm | ||
− | export CROSS_COMPILE=arm- | + | export CROSS_COMPILE=arm-linux-gnueabihf- |
+ | export PATH=$PATH:~/BeagleBoard/bb-kernel/dl/gcc-linaro-7.1.1-2017.08-x86_64_arm-linux-gnueabihf/bin | ||
+ | Make sure the PATH you use goes to the bin directory where the cross compiler is installed. | ||
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 ~ | + | host$ '''source ~/crossCompileEnv.sh''' |
host$ '''${CROSS_COMPILE}gcc helloWorld.c''' | host$ '''${CROSS_COMPILE}gcc helloWorld.c''' | ||
host$ '''file a.out''' | 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. | + | a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), |
+ | dynamically linked (uses shared libs), for GNU/Linux 2.6.32, | ||
+ | BuildID[sha1]=b9222cbcee442470c7b89ac294e392a631c82264, 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 | 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 | + | host$ '''scp a.out debian@192.168.7.2:.''' |
− | host$ '''ssh | + | host$ '''ssh debian@192.168.7.2 ./a.out''' |
Hello, World! Main is executing at 0x8374 | Hello, World! Main is executing at 0x8374 | ||
This address (0xbeb32d4c) is in our stack frame | This address (0xbeb32d4c) is in our stack frame | ||
Line 49: | Line 56: | ||
This address (0x10648) is in our data 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 | + | The '''scp''' copies ''a.out'' to the beagle and the '''ssh''' runs the ''a.out'' on the beagle. Notice the addresses are very different from the host version. |
{{YoderFoot}} | {{YoderFoot}} |
Revision as of 06:14, 28 September 2017
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_4.4 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
Normally when you compile you compile on the machine that will run the code. You can compile and run on the Bone, but sometimes (like when compiling the kernel) it's better to use a more powerful machine for the compiling. First we'll compile helloWorld.c on the host computer and run it there, then we'll cross compile it on the host to run on the Bone.
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$ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x357e34e90f7c32d414368d69cc06d0aed59acf1c, not stripped 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 figure out what version of the cross compiler was loaded.
host$ cd BeagleBoard/bb-kernel/dl host$ ls gcc-linaro-5.4.1-2017.05-x86_64_arm-linux-gnueabihf gcc-linaro-5.4.1-2017.05-x86_64_arm-linux-gnueabihf.tar.xz gcc-linaro-7.1.1-2017.08-x86_64_arm-linux-gnueabihf gcc-linaro-7.1.1-2017.08-x86_64_arm-linux-gnueabihf.tar.xz
Here we see two versions of the compiler have been loaded. Let's pick the newer one.
Now set the paths to find the cross-compiler. Put the following in a file, call it ~/crossCompileEnv.sh.
export ARCH=arm export CROSS_COMPILE=arm-linux-gnueabihf- export PATH=$PATH:~/BeagleBoard/bb-kernel/dl/gcc-linaro-7.1.1-2017.08-x86_64_arm-linux-gnueabihf/bin
Make sure the PATH you use goes to the bin directory where the cross compiler is installed.
Now source the file and compile again. (Note: you only have to source once per terminal session.)
host$ source ~/crossCompileEnv.sh host$ ${CROSS_COMPILE}gcc helloWorld.c host$ file a.out a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=b9222cbcee442470c7b89ac294e392a631c82264, 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 debian@192.168.7.2:. host$ ssh debian@192.168.7.2 ./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 addresses are very different from the host version.
Embedded Linux Class by Mark A. Yoder