一、配置RaspberryPi交叉編譯環境:html
在開發RaspberryPi Zero的過程當中,因爲Zero板卡的CPU的處理性能比較弱,所以其編譯的性能比較弱,須要將代碼在PC電腦上交叉編譯完成以後再拷貝到Zero上調試運行。配置交叉編譯環境的基本步驟以下所示:linux
Step1:從github網站獲取raspberryPi交叉編譯工具包:https://github.com/raspberrypi/toolsgit
git clone git@github.com:raspberrypi/tools.git
上述文件下載完以後的內容以下所示(其中arm-bcm2708文件夾中的內容爲交叉編譯器):github
Step2:將交叉編譯器配置到環境變量:ubuntu
編輯環境變量配置文件.bashrc,在文件的末尾添加以下的代碼(注意路徑,不一樣的路徑記得修改):bash
export PATH=$PATH:~/Raspberry_Project/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
配置完成以後,須要執行source指令,讓.bashrc生效:工具
source ~/.bashrc
Step3:測試交叉編譯環境性能
以下圖所示,在ubuntu命令行中輸入以下指令:測試
arm-linux-gnueabihf-gcc -v
有此圖說明配置成功。網站
二、交叉編譯執行main.c文件:
Step1:編寫了以下的測試文件:
1 #include <stdio.h> 2 3 int main(void) 4 { 5 printf("Test the cross-compile-env for raspberrypi!\n"); 6 return 1; 7 }
Step2:使用以下的指令編譯上述文件:
arm-linux-gnueabihf-gcc -c main.c -o main
編譯的結果以下:
將編譯生成的main文件拷貝到樹莓派上,修改執行權限,執行便可。
三、交叉編譯過程當中須要連接第三方的庫文件:
在編寫程序的過程當中,咱們須要使用到wiringPi庫相關的內容,所以須要將wiringPi的文件引入。
Step1:首先下載wiringPi的程序包:https://github.com/mm1994uestc/WiringPi。能夠經過github直接下載源代碼:
git clone git@github.com:mm1994uestc/WiringPi.git
下載完成以後進入文件夾安裝wiringPi的庫,安裝方法參考這裏:http://www.javashuo.com/article/p-mzpydapv-q.html
注:安裝過程當中若是出現問題,請使用sudo權限操做,sudo make install
重點:將wiringPi安裝在樹莓派以後,咱們能夠在其編譯文件夾下面找到編譯成功的wiringPi庫程序或者安裝在/usr/lib當中,以下所示:
libwiringPi是基本的wiringPi程序庫
咱們能夠將這些文件拷貝到Ubuntu環境下,爲交叉編譯作好準備,以下所示:
a. devLib:Raspberry basical hardware driv.
b. examples:Some examples for the wiringPi Lib.
c. gpio:gpio demo to show the control of gpio.
d. libwiringPi.so:Baisic wiringpi library usefull when you are compile the c code.
e. main:Executiable file for Raspberry.
f. main.c:The c source code.
g. Makefile:Used to compile the C Code into executibale binary file.
h. wiringPi:The source Code of wiringPi(include .c & .h file),when you are compile the code,it needed.
i. wiringPiD:Other devices Library Source Code if you used it,you need to include the lib and .h file.
Step2:安裝拷貝完成以後咱們編寫以下的程序:
#include <stdio.h> #include <stdlib.h> #include "wiringPi.h" int main(int argc, char **argv) { if(argc<2){ printf("You should give a param while call the program1\n"); return 0; } int pinNumber = atoi(argv[1]); printf("The control pin number is:%d\n",pinNumber); if(-1 == wiringPiSetup()){ printf("Setup wiringPi failed!\n"); return 0; } pinMode(pinNumber, OUTPUT); while(1){ digitalWrite(pinNumber,0); delay(800); digitalWrite(pinNumber,1); delay(800); } printf("Hello, you do it succeed!!!\n"); return 0; }
Step3:編譯上述程序,這裏編寫了簡單的Makefile文件,以下所示:
main:main.c arm-linux-gnueabihf-g++ main.c -o main -L. -lwiringPi -IwiringPi clean: rm main
注:這裏的路徑-I是wiringPi的安裝路徑。必定要應用wiringPi的相關文件的路徑才能調用wiringPi的庫,觸類旁通,其餘的庫也能夠相似的方式,對於靜態的library,咱們可使用-l的方式來連接,若是在RaspberryPi的本地,咱們可使用以下的方式(這裏連接了math數學庫&wiringPi庫):
gcc -c main.c -o main -lwiringPi -lm
-L.的意義表示,程序調用的庫在當前文件目錄下,若是再也不請觸類旁通,設置Library庫的絕對路徑。-I的意思是include的意思,將wiringPi的.h文件包括進來。
關於Makefile連接相關的內容參考這裏:http://www.javashuo.com/article/p-tpinvsqj-cn.html
Step4:make生成RaspberryPi的可執行文件,經過scp拷貝到Raspberry執行便可:
四、Reference:
交叉編譯參考:http://www.javashuo.com/article/p-umpypefm-bq.html
wiringPi官網:http://wiringpi.com/