2016年3月24日 星期四

Raspberry Pi - 下載安裝 toolchain

1. 安裝相關套件
$ sudo apt-get install make git-core ncurses-dev
2. 從Git Server下載toolchain
$ mkdir rpi
$ cd rpi
$ git clone https://github.com/raspberrypi/tools.git
3. 設定環境變數
$ vi ~/.bashrc
export PATH=$PATH:/home/allen/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin

如果編譯環境的主機是64位元
export PATH=$PATH:/home/allen/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
4. 編譯一個簡單的範例
$ vi hello.c
#include <stdio.h>
int main()
{
        printf("hello world\n");
        return 0;
}
$ arm-linux-gnueabihf-gcc hello.c -o hello-arm
5. 用file命令驗證編譯後的binary
$ file hello-arm
hello-arm: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, not stripped

// 因為為arm平台的binary,所以在x86平台無法執行
$ ./hello-arm 
bash: ./hello-arm: cannot execute binary file
6. 把binary傳送到Raspberry pi上,然後執行看結果
$ scp hello-arm pi@192.168.1.2:/home/pi
pi@192.168.1.2's password: 
hello-arm                                                                                                                             100% 5447     5.3KB/s   00:00

$ ./hello-arm
hello, world

沒有留言:

張貼留言