---
# System prepended metadata

title: Cross Compile Madplay
tags: [Embedded System Design]

---

# Cross Compile Madplay

* 參考這本書Chapter 7.4：[ARM 嵌入式系统移植实战开发](http://www.wenqujingdian.com/Public/editor/attached/file/20180317/20180317215634_64129.pdf)
* http://www.wenqujingdian.com/Public/editor/attached/file/20180317/20180317215634_64129.pdf
* 裡面有些指令有錯，因此下面的command有做修正

## Cross Compile 需要的庫

* 解壓所需的文件
```
mkdir /usr/local/madplay-source
tar zxvf zlib-1.2.3.tar.gz -C /usr/local/madplay-source/
tar zxvf libid3tag-0.15.1b.tar.gz -C /usr/local/madplay-source/ 
tar zxvf libmad-0.15.1b.tar.gz -C /usr/local/madplay-source/ 
tar zxvf madplay-0.15.2b.tar.gz -C /usr/local/madplay-source/
```
### zlib
```
mkdir /usr/local/mymadplay/zlib-1.2.3
./configure -shared --prefix=/usr/local/mymadplay/zlib-1.2.3
```

```
gedit Makefile
```
* 修改下列參數：
* AR=arm-linux-gnueabihf-ar rc: 這用於指定靜態庫（archive）的創建命令，使用 arm-linux-gnueabihf-ar 創建一個靜態庫。
* RANLIB=arm-linux-gnueabihf-ranlib：這指定了庫索引（library index）的創建命令，即使用 arm-linux-gnueabihf-ranlib 創建一個庫的索引。
```
CC = arm-linux-gnueabihf-gcc \
LDSHARED=arm-linux-gnueabihf-gcc \
-shared -Wl,-soname,libz.so.1 \
CPP=arm-linux-gnueabihf-gcc -E \
AR=arm-linux-gnueabihf-ar rc \
RANLIB=arm-linux-gnueabihf-ranlib
```

```
make
make install
```

### libid3tag

* 修改配置
* --prefix=/usr/local/madplay-source/libid3tag: 這個選項指定了軟件包的安裝目錄，即將編譯後的文件安裝到 /usr/local/madplay-source/libid3tag 目錄中。
* CC：使用交叉編譯器 arm-linux-gnueabihf-gcc 來進行編譯
* --host=arm-linux-gnueabihf：這指定了目標主機的體系結構，
* CPPFLAGS=-I：指定了頭文件(include)的搜索路徑
* LDFLAGS=-L：指定鏈接器的選項，具體來說是指定了庫文件（library）的搜索路徑，
```
./configure --prefix=/usr/local/madplay-source/libid3tag \
CC=arm-linux-gnueabihf-gcc --host=arm-linux-gnueabihf  \ 
CPPFLAGS=-I/usr/local/mymadplay/zlib-1.2.3/include/ \
LDFLAGS=-L/usr/local/mymadplay/zlib-1.2.3/lib/ \
make
make install
```

### libmad
```
./configure --prefix=/usr/local/madplay-source/libmad \
CC=arm-linux-gnueabihf-gcc \
--host=arm-linux-gnueabihf \
CPPFLAGS=-I/usr/local/mymadplay/libid3tag/include/ \
LDFLAGS=-L/usr/local/mymadplay/libid3tag/lib  \
make
```
* [報錯按這篇修改](https://www.twblogs.net/a/5e6b933dbd9eee211685f17d?fbclid=IwAR1a9ZtyoccMP_94uWY)
* https://www.twblogs.net/a/5e6b933dbd9eee211685f17d?fbclid=IwAR1a9ZtyoccMP_94uWY
```
gedit Makefile
//remove -fforce-mem
```
```
gedit fixed.h
將
# define MAD_F_MLN(hi, lo) \
asm ("rsbs %0, %2, #0\n\t" \ "rsc %1, %3, #0" \
: "=r" (lo), "=r" (hi) \
: "0" (lo), "1" (hi) \
: "cc")
改爲
#ifdef __thumb__
/* In Thumb-2, the RSB-immediate instruction is only allowed with a zero operand. If needed this code can also support Thumb-1
(simply append "s" to the end of the second two instructions). */
# define MAD_F_MLN(hi, lo) \
asm ("rsbs %0, %0, #0\n\t" \
" sbc %1, %1, %1\n\t" \
"sub %1, %1, %2" \
: "+&r" (lo), "=&r" (hi) \ : "r" (hi) \
: "cc")
#else /* ! __thumb__ */
# define MAD_F_MLN(hi, lo) \
asm ("rsbs %0, %2, #0\n\t" \ "rsc %1, %3, #0" \
: "=r" (lo), "=r" (hi) \
: "=&r" (lo), "=r" (hi) \ : "0" (lo), "1" (hi) \
: "cc")
#endif /* __thumb__ */

```
```
make
make install
```

### madplay
```
./configure \
--prefix=/usr/local/mymadplay/madplay-0.15.2 \ 
CC=arm-linux-gnueabihf-gcc \ 
--host=arm-linux-gnueabihf \
CPPFLAGS="-I/usr/local/madplay-source/libid3tag/include/ -I/usr/local/madplay-source/libmad/include/" \
LDFLAGS="-L/usr/local/mymadplay/zlib-1.2.3/lib/ \
-L/usr/local/madplay-source/libid3tag/lib/ \
-L/usr/local/madplay-source/libmad/lib/"
make
make install
```
* 確認執行所需要的依賴
```
arm-linux-gnueabihf-readelf -d madplay | grep Shared 
#
Shared library: [libmad.so.0] 
Shared library: [libid3tag.so.0]
Shared library: [libm.so.6]
Shared library: [libc.so.6]
Shared library: [ld-linux-armhf.so.3]
```
* 確認庫的連結
```
cd /usr/local/madplay-source/libmad/lib
ls -l /usr/local/madplay-source/libmad/lib/libmad.so.0
# lrwxrwxrwx 1 root root 15 11月 24 09:18 /usr/local/madplay-source/libmad/lib/libmad.so.0 -> libmad.so.0.2.1
ls -l /usr/local/madplay-source/libid3tag/lib/libid3tag.so.0
# lrwxrwxrwx 1 root root 18 11月 24 09:09 /usr/local/madplay-source/libid3tag/lib/libid3tag.so.0 -> libid3tag.so.0.3.0
```
### aplay
* [參考資料](http://www.armbbs.net/home.php?mod=space&uid=4351&do=blog&id=260&mobile=no)
* http://www.armbbs.net/home.php?mod=space&uid=4351&do=blog&id=260&mobile=no
* 流程與上述都差不多，交叉編譯後，把東西與依賴上傳到板子上
```
/configure --host=arm-linux-gnueabihf \
--prefix=/home/book/downloads/madplay/arm-alsa \
--enable-static \
--disable-shared \
--disable-python \
--disable-alsamixer \
--disable-xmlto \ --with-configdir=/usr/local/share/alsa \ --with-plugindir=/usr/local/lib/alsa_lib \ CC=arm-linux-gnueabihf-gcc \ CXX=arm-linux-gnueabihf-g++ \ LD=arm-linux-gnueabihf-ld
make
make install
```
* 測試aplay
```
aplay a.wav
```

### 將madplay移植到開發版

1. 將 PC 機的libmad.so.0.2.1和libid3tag.so.0.3.0 兩個庫檔案拷貝到開發板的/lib目錄下
2. 建立軟連結檔案 libmad.so.0 和 libid3tag.so.0 在開發板終端使用以下命令建立: 
```ln -s libid3tag.so.0.3.0 libid3tag.so.0
ln -s libmad.so.0.2.1 libmad.so.0
```
3. 查看移植是否成功
在開發板終端機輸入指令
```
ls -l libmad.so.0 libid3tag.so.0
```
如果出現以下結果則表示移植成功: 
```
lrwxrwxrwx1root root 18Mar 909:58libid3tag.so.0 -> libid3tag.so.0.3.0
lrwxrwxrwx 1root root 15Mar 909:58libmad.so.0 -> libmad.so.0.2.1
```
4. 移植 madplay應用程式到開發板
把/usr/local/mymadplay/madplay-0.15.2/bin 目 錄 下 的 可 執 行 文 件 madplay 複製到開發板/usr/bin下面,再傳一首.mp3格式的音樂檔案到開發板/mnt目錄。
5. 測試 madplay是否移植成功
在開發板控制終 端 下 進 入/mnt目 錄,使 用 “命 令 madplay+ 文 件 名”的 形 式 播 放 mp3格式的音樂文件,如果成功播放則說明移植 madplay成功。
```
madplay -o wav:a.wav a.mp3 & aplay a.wav
```



