In this blog entry the setup of a cross-compiling development environment for the Raspberry Pi will be demonstrated. This will include the
We will finally write a simple Hello World program on our Kubuntu virtual machine (VM), compile it using the cross compiler and then deploy it onto our Raspberry Pi board to run it.
I’m going to assume that you have already installed a Raspbian Wheezy image on your RPi board and that you have Ubuntu / Kubuntu Linux installed either has a host OS or guest OS on your desktop PC.
A remote debugging tutorial; which I consider to be the continuation of this tutorial, can be found here.
So what is a cross compiling toolchain and why use one ?
A native compiler such as the default gcc tool on Kubuntu is a compiler that runs on an Intel machine, as well as creates binaries intended to be run on an Intel machine. i.e it creates binaries for the same type of machine that it runs on. Similarly the GCC tool in the RPi’s Raspbian Linux OS is intended to run on an ARM machine as well as creates binaries for an ARM machine.
A cross compiler such as the “arm-linux-gnueabihf-gcc” that we will use is able to run on an Intel machine but creates binaries for an ARM machine. In other words, it runs on one architecture and creates binaries for another. This allows us to develop and compile our programs on our Desktop PC but when it comes to deploying the binaries we deploy them and run them on the Raspberry Pi.
So why use a Cross-compiler instead of developing our code and compiling it natively on the Raspberry Pi itself? After all, the Raspberry Pi has a native GCC compiler. We can also use code editors such as nano or vi from the command line (remotely over SSH) or GUI programs such as Geany (remotely over VNC).
The main case for cross-compilation over native compilation (develop and compile on the RPi itself) is that it tends to be faster. Remember the RPi board may be fast compared to a microcontroller…but its still has limited RAM resources and is pretty slow compared to an average desktop computer….(在宿主机上开发会更快更流畅,RPI本质上还是一个资源不足的电脑,性能远低于我们自己的桌面电脑)
Also you have a myriad of development tools that you can use on your desktop PC that you simply can’t use on the Raspberry Pi; such as the Eclipse IDE.
Now that I’ve explained the why…let’s get started!
Downloading and Setting Up the Cross Compiling Toolchain
Of those three we will use the third one. The next step is to add the directory containing the binary files of the third toolchain “gcc-linaro-arm-linux-gnueabihf-raspbian” to the PATH environment variable in linux. This way we can access the toolchain’s binary files from anywhere on our computer. We will do this by adding an “export PATH” command to the bottom of the .bashrc and .profile files in the home directory.(将交叉编译工具链的执行文件路径写入环境变量,这样就可以在系统里的任何地方都可以调用该工具了)
“export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin”
“export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin”
Now restart your machine. (You probably could just log out and log in again)
Ideally you need to export the toolchain’s directory path to the PATH variable in either the .bashrc or the .profile files. This didn’t work for me. If it works for you thats great.
When you log-in into your desktop Linux OS, open a new console window and type: ” arm-linux-gnueabihf-gcc -v”. If you were successful you should see output similar to that in Figure 1. Congratulations! you just installed Raspbian’s official cross compiling toolchain on your Desktop PC / Virtual Machine!
Figure 1. Cross Compiling Toolchain successfully installed and accessible from anywhere within your Desktop side Linux OS!!!
联系客服