Monday, December 17, 2012

Cross Compiling to Raspberry Pi from Windows 7


In this post I will explain how to cross compile a "Hello World!" application from Windows 7 to Raspberry Pi. There's a catch though, I run the cross compiler in Linux Slackware 12 (x86) running inside virtual machine in Windows 7 host :P.

So, first up is prepare the Slackware 12 virtual machine in Windows 7. I used Vmware, but you could use VirtualBox as well. Once you get that running, you can now start preparing for the cross compilation. Download the cross toolchain from: https://github.com/raspberrypi/tools, then decompress it with unzip tool in the shell as follows:
pinczakko@sharingan:~/unzip sources/cross-tools-master.zip -d raspberry_pi_cross_tools
with the command above, the cross-toolchain is now at
~/raspberry_pi_cross_tools
Now, I need to export the path of the cross compilers to my local environment. The path of the cross compilers binaries is at
~/raspberry_pi_cross_tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
at this point. To add the cross compilers to the environment, I edited
~/.profile 
and
~/.bashrc 
to include the following line in the end of both files:
export PATH=$PATH:$HOME/raspberry_pi_cross_tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
After both of the files edited, you have to log out and the log in again to make the environment changes recognized (actually, changing only .profile suffice for our purposes in Slackware 12). Now, once you log in again, you can test whether the environment changes are in effect by invoking one of the compilers. For example:
pinczakko@sharingan:~$ arm-linux-gnueabihf-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/pinczakko/raspberry_pi_cross_tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../libexec/gcc/arm-linux-gnueabihf/4.7.2/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: /cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/src/gcc-linaro-4.7-2012.08/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-linux-gnueabihf --prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/install --with-sysroot=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fortran --disable-multilib --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08' --with-bugurl=https://bugs.launchpad.net/gcc-linaro --enable-__cxa_atexit --enable-libmudflap --enable-libgomp --enable-libssp --with-gmp=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-ppl=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-libelf=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-host-libstdcxx='-L/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static/lib -lpwl' --enable-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gold --with-local-prefix=/cbuild/slaves/oort61/crosstool-ng/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08)
Now I have the cross compiler added to path in the bash shell. I proceed to compile a very simple hello world C application. This is the source code of that application:
#include < stdio.h >

int main(int argc, char** argv)
{
 printf("Hello World! [cross gcc]\n");
 return 0;
}
Invoke this command to compile this source file (I named the file cross_test.c):
arm-linux-gnueabihf-gcc -o cross_test cross_test.c
This creates a binary named cross_test. I transfer the binary to the Raspberry Pi with WinSCP and proceed to run it there. This is the result (in PuTTy).

This should be the first step in cross compiling to Raspberry Pi. I know that there are some Windows cross-compilers for Raspberry Pi out there but I have yet to test them.
Post a Comment

No comments: