2014年9月3日 星期三

Cross Compiler for Raspberry Pi

Speaking to embedded system, you have to know about cross compiler, toolchain and CMake. Cross Compiler is a compiler which could create the executable code for a platform other than the one which the compiler is running. You don't want to compile your huge source code on the embedded system where maybe took lots of time. In order to set up the environment, toolchain consisting of a compiler and linker could transform the source code into a executable program, libraries to provide interfaces to the operating system and a debugger. In this case, CMake will help you figure out the environment of the platform via toolchain. Basically, you could compile your source code to executabl program which could execute on the Raspberry Pi by these three main tools. It will save your lots of time and more convenient for further processing. Let's see how it works.

Before the beginning of the process, make sure that you have install CMake, git, rsync 
(sudo apt-get install cmake git rsync)

Install Cross Compilation toolchain
At first, you have to download a pre-built cross compilation tool chain for a slightly different version of gcc and libc :




And install the toolchain by adding the environment variable. 
sudo nano ~/.bashrc  and add below line at the end of file.
export PATH=$PATH:/home/gtossk2/Desktop/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin










(After that, you enter "ar" characters and enter tab twice. You see something like arm-linux-gnueabihf-gcc which means successfully intallation.)


Make Raspberry Pi development files (header files, libraries) available on the host
mkdir mnt_rpi and cd mnt_rpi    Now tell rsync to copy the entirely of the /usr and /lib directories of the Raspberry Pi file system into the newly created folder on your computer.






After that, there is a important thing to be done. The files /usr/lib/arm-linux-gnueabihf/libc.so and /usr/lib/arm-linux-gnueabihf/libpthread.so are actually linker scripts refer to absolute paths, which are incorrect when cross compiling. Please remove the absolute path from linker script.

For example :
Becomes :





Make a cmake toolchain file for cross compilation
Now create a toolchain for cmake to set up the environment. Name it as Toolchain-RaspberryPi.cmake in path/rpi

For example :















Cross compile on Ubuntu
Now you can use Toolchain-RaspberryPi.cmake for cross-compiling.
For example :

Note : you have to set CMAKE_EXE_LINKER_FLAGS cause arm-linux-gnueabihf-ld could not recognize the sysroot. You have to set --sysroot=/home/gtossk2/Desktop/rpi/mnt_rpi for CMAKE_EXE_LINKER_FLAGS


Or you will get some linker errors :


Run on the Raspberry Pi
Using scp to transfer the executable file to Raspberry Pi and execute it on Raspberry Pi. See how it works. 






Reference:



2 則留言:

  1. If you cannot find the .h header (fatal error: SDL/SDL.h: No such file or directory #include )
    In your CMakeLists.txt, add the command to specify the path of .h header

    INCLUDE_DIRECTORIES(/home/gtossk2/Desktop/rpi/mnt_rpi/usr/local/include
    /home/gtossk2/Desktop/rpi/mnt_rpi/usr/include)

    回覆刪除
  2. cc1plus: error: unrecognized argument in option ‘-mtune=native’

    Remove "-mtune=native" in CMakeLists.txt. It cannot work on cross compile...

    回覆刪除