[GCC]_將cmatrix原始碼編譯成ARM(Raspberry Pi2)及x86 32Bit(Intel Galileo)可用的執行檔

最近在練習gcc以及cross compiler的操作,開始比較認真搞懂gcc的參數跟compiler的運作流程

 

但這裡只筆記我遇到的問題,算是各種平台的compile練習

用cmatrix當作compile的source是因為他是一個單純的C++ Code,並且需要引用一個ncurses library.

 

兩個Cross compile的目標為Raspberry pi2, 搭載raspbian

以及Intel Galileo Gen2, 搭載porting過去的Debian 7

 

本文分類比較多,大綱如下:

I. X64 Ubuntu 15.04 compile to self origin platform.

II. X64 Ubuntu 15.04 cross compile to ARM(Raspberry Pi2).

III. ARM(Raspberry Pi 2) compile to self origin platform.

V. X64 Ubuntu 15.04 compile to X86(32bit) Intel Galileo Gen2.(目前拿去Galileo未成功,會有Illegal instruction,應該還是需要特定的compiler)

VI. X86(32bit) Intel Galileo Gen2 compile to self origin platform.

 

O. 前置作業

後面個平台自己都要做,各區段不重複提了

  1. 取得cmatrix source code

wget http://www.asty.org/cmatrix/dist/cmatrix-1.2a.tar.gz

 

  1. 解壓縮

tar xzvf cmatrix-1.2a.tar.gz

 

  1. 安裝編譯需要的檔案

sudo apt-get install build-essential

 

4.進入cmatrix source資料夾

cd cmatrix-1.2a

 

5.執行configure

./configure

 

另外筆記一下,不需要現在做,scp指令:

scp cmatrix root@192.168.41.27:/root/

為將cmatrx檔案透過遠端root身分複製到遠端的/root資料夾底下。

 


 

I. X64 Ubuntu 15.04 compile to self origin platform.

 

  1. 需要安裝libncurses

sudo apt-get install libncurses5-dev

 

2-1. 直接Compile,就得到名為cmatrix執行檔,可以直接執行

gcc  -g -O2 -Wall -Wno-comment cmatrix.c -o cmatrix -lncurses

 

2.你也可以用正規的方式來make,得到執行檔

make

 

你可以選擇make install裝入系統,其實就是幫你放到/usr/local底下跟細節一些man doc.

接下來的其他編譯平台都不透過Make檔編譯以及安裝,只產生最後的執行檔。

 

透過file cmatrix得到結果:

cmatrix: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0edf094a1576bd95f0d8bd9e187a4781b30bf465, not stripped

 


 

II. X64 Ubuntu 15.04 cross compile to ARM(Raspberry Pi2).

 

1.清除一下上一階段的物件檔跟編譯完成的執行檔

make clean

 

2.安裝arm用的compiler(RPI2兩個架構都可以,官方是建議HF,則一或者兩者都可以,最後要選定一個架構的gcc compile)

ARMHF

sudo apt-get install gcc-arm-linux-gnueabihf
sudo apt-get install g++-arm-linux-gnueabihf

ARMEL

sudo apt-get install gcc-arm-linux-gnueabi
sudo apt-get install g++-arm-linux-gnueabi

 

3.抓ncurses原始擋下來,準備利用arm compiler先compiler ncurses libary (引用參考至;http://soft-dev-pro.blogspot.tw/2014/07/cross-compile-ncurses-for-arm.html)

wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz

 

4.解壓縮

tar xzvf http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz

 

5.進入資料夾

cd ncurses-5.9

 

6.編譯及安裝ncurses libary for arm

#ARMEL
./configure –host=arm-linux-gnueabi –prefix=/usr/arm-linux-gnueabi CXX=”arm-linux-gnueabi-g++”
#ARMHF
./configure –host=arm-linux-gnueabihf –prefix=/usr/arm-linux-gnueabihf CXX=”arm-linux-gnueabihf-g++”
make
sudo make install

 

7.檢查一下是否裝妥,若前面兩個架構都有做,那麼底下都會有libncurse.a

ls /usr/arm-linux-gnueabi/lib
ls /usr/arm-linux-gnueabihf/lib

 

8.透過arm-linux-guneabihf-gcc編譯得到執行檔

arm-linux-gnueabihf-gcc -g -O2 -Wall -Wno-comment cmatrix.c -o cmatrix -lncurses

 

透過file cmatrix得到結果:

cmatrix: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=32381545b9b8699e51febd137a6d7fc1f9ff54d1, not stripped

 


 

III. ARM(Raspberry Pi 2) compile to self origin platform.

接著是在RPI2上面自己編譯

 

1.也是需要補完ncurses libary

sudo apt-get install libncurses5-dev

 

2.直接透過RPI2上的gcc編譯(是for arm的),得到執行檔

gcc -g -O2 -Wall -Wno-comment -o cmatrix cmatrix.c -lncurses

或用

arm-linux-gnueabihf-gcc -g -O2 -Wall -Wno-comment -o cmatrix cmatrix.c -lncurses

是一樣的,gcc其實就是用arm-linux-gnueabihf-gcc

 

透過file cmatrix得到結果:

cmatrix: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0xfecf796f05fe19526c2a579be2a6fa1b379ffe85, not stripped

 


 

V. X64 Ubuntu 15.04 compile to X86(32bit) Intel Galileo Gen2.

(目前拿去Galileo未成功,會有Illegal instruction,應該還是需要特定的compiler)

接著用電腦編譯32位元給galileo,其實架構一樣都是x86,只是位元是32bit,所以多一個參數-m32

 

1.記得清除上一階段的結果

make clean

 

2.要安裝32bit的ncurses

sudo apt-get install lib32ncurses5-dev

 

3.透過-m32編譯成32bit程式

gcc -m32 -g -O2 -Wall -Wno-comment cmatrix.c -o cmatrix -lncurses

 

如果遇到錯誤如下:

In file included from /usr/include/sys/ioctl.h:26:0,
                 from cmatrix.c:41:
/usr/include/bits/ioctls.h:23:24: fatal error: asm/ioctls.h: No such file or directory

 

蠻奇怪的…第一次做的時候都沒有,後來撰文重新做一次就出現…

後來在stackoverflow找到解法:http://stackoverflow.com/questions/14795608/asm-errno-h-no-such-file-or-directory

 

可以下sudo ln -s /usr/include/asm-generic /usr/include/asm

再重新compile一遍就會過了~

 

透過file cmatrix得到結果:

cmatrix: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=6952c300beb74664b0244109cac11ffc3314f15d, not stripped

 


 

VI. X86(32bit) Intel Galileo Gen2 compile to self origin platform.

 

1.也是需要補完ncurses libary

sudo apt-get install libncurses5-dev

 

2.直接透過上頭的gcc編譯,得到執行檔

gcc -g -O2 -Wall -Wno-comment cmatrix.c -o cmatrix -lncurses

 

透過file cmatrix得到結果:

cmatrix: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0xa61c77849cd82567baa1cb6a8a33a9f3bec2be73, not stripped

 


 

 

以上就是針對兩種不一樣的平台做cross compiler的練習~

 

Leave a comment

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料