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.
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:
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)
cc1plus: error: unrecognized argument in option ‘-mtune=native’
回覆刪除Remove "-mtune=native" in CMakeLists.txt. It cannot work on cross compile...