jollen.org

Jollen 的 Blog
Jollen's email: jollen # jollen.org
more:  Jollen's Training

[教育訓練紀錄] 交叉編譯(cross compile)thttpd

.作者:jollen/
.日期:March 30, 2008 10:54 AM


本週進行 root filesystem 相關的教育訓練,今天給的課堂練習是 thttpd 的交叉編譯(cross compile)。thttpd 採用標準的 GNU autoconf 來產生 Makefile,因此,交叉編譯 thttpd 的方式是蠻簡單的。配合課堂提供的 cross toolchain(gcc 3.4.1),我們先定義以下有關 cross toolchain 路徑檔檔名的 Makefile 變數:

TOOL_TOP = /opt/crosstool/gcc-3.4.1-glibc-2.3.3/arm-9tdmi-linux-gnu
CC = $(TOOL_TOP)/bin/arm-9tdmi-linux-gnu-gcc
AR = $(TOOL_TOP)/bin/arm-9tdmi-linux-gnu-ar
LD = $(TOOL_TOP)/bin/arm-9tdmi-linux-gnu-ld
AS = $(TOOL_TOP)/bin/arm-9tdmi-linux-gnu-as
STRIP = $(TOOL_TOP)/bin/arm-9tdmi-linux-gnu-strip
RANLIB = $(TOOL_TOP)/bin/arm-9tdmi-linux-gnu-ranlib
TARGET_ARCH = arm-linux

接著再定義與 thttpd 的 'CFLAGS' 參數:

THTTPD_CFLAGS = -Os -Wall -mtune=arm9tdmi -march=armv4 \
                -fomit-frame-pointer -fsigned-char -fPIC

我們希望能有一個比較彈性以及系統化的做法,因此採用 Makefile 的系統來實作,不考慮編寫 script 的方式。接著,再定義一個 target,用來設定 thttpd 的 autoconf:

configured-thttpd:
        (export CC=$(CC); \
        export AR="$(AR)"; \
        export AS=$(AS); \
        export LD=$(LD); \
        export STRIP=$(STRIP); \
        export RANLIB=$(RANLIB); \
        export CFLAGS="$(THTTPD_CFLAGS)"; \
        ./configure \
                --prefix=/ \
                --host=$(TARGET_ARCH) );

我們想要用 'install' 指令來安裝 thttpd 執行檔到 root filesystem,因此 '--prefix' 參數的定義對我們講並不重要。Makefile 有良好的 dependencies 系統,所以我再撰寫一段 rule 如下:

thttpd: configured-thttpd
        make

接著,將以上的 Makefile 內容存檔,例如存成 thttpd.cross,再將這個檔案放到 thttpd 原始碼根目錄下,執行 make 並引用 thttpd.cross 的 'thttpd' target:

$ make -f thttpd.cross thttpd

一轉眼,我們得到 thttpd 的執行檔了。再將此 thttpd 執行檔加到 root filesystem 裡即可,別忘了,thttpd 需要幾個 shared library,把他們也加到 root filesystem 裡!還有,thttpd 會去讀取 user database(/etc/passwd 等),所以也要把 NSS 的 files 程式庫(libnss_files.so)加到 root filesystem 裡!

利用 Makefile 系統取代直接敲命令(或編寫 script)的麻煩做法,讓整個過程看起來簡單又清楚!

Tags:

純手工打造說明:技術專欄文章為 Jollen 原創,內容皆為人工撰寫,無 AI 生成。轉載請註明出處與作者,並全文引用。轉載時請在文章開頭或結尾明顯處註明「本文出處:https://www.jollen.org,已取得原作者同意並授權使用。」
訂閱電子報:不定期 Jollen's Blog 精選文章隨 Moko365 電子報寄送;請透過 Moko365 電子報訂閱(可隨時取消)。

Copyright(c) 2001–2008 www.jollen.org. All rights reserved.
Last update: 2026-07-22