本文以新增一個最簡單的 hello 程式為例,說明在 uClinux 原始碼樹中註冊自訂應用程式、撰寫 Makefile 與原始碼,最後編進 romfs 的完整五個步驟。
編輯 uClinux-dist/user/Makefile,加入條件式判斷:
dir_$(CONFIG_USER_HELLO_WORLD) += hello
這行的意思是:當設定選項 CONFIG_USER_HELLO_WORLD 被開啟時,才把 hello 這個子目錄納入編譯。
編輯 uClinux-dist/config/Configure.help:
CONFIG_USER_HELLO_WORLD A simple hello world program
這段文字會顯示在 make menuconfig 的說明區,讓使用者知道該選項的用途。
編輯 uClinux-dist/config/config.in,找到這兩行:
mainmenu_option next_comment comment 'Miscellaneous Applications'
在它們之後加上:
bool 'hello' CONFIG_USER_HELLO_WORLD
如此 hello 就會出現在「Miscellaneous Applications」分類底下。
uClinux-dist/user/hello/Makefile:
EXEC = hello OBJS = hello.o all: $(EXEC) $(EXEC): $(OBJS) $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) romfs: $(ROMFSINST) /bin/$(EXEC) clean: -rm -f $(EXEC) *.elf *.gdb *.o
其中 romfs 這個目標很關鍵,它透過 $(ROMFSINST) 把編譯好的執行檔放進 romfs 的 /bin 底下,這也是程式能在 uClinux 上被執行到的地方。
uClinux-dist/user/hello/hello.c:
#include <stdio.h>
int main()
{
printf("Hello, World\n");
return 0;
}
cd ~/uClinux-dist make menuconfig # 在 Miscellaneous Applications 中勾選 hello make dep make romfs
完成後,hello 就會被放進 romfs 映像的 /bin 目錄,燒錄或載入新映像即可在目標板上執行。
實務上要注意兩點:uClinux 的交叉編譯工具鏈多為 uClibc,若程式用到 glibc 專屬功能可能無法直接通過編譯;另外若要在設定選單中分類或加上相依條件,除了 config.in 之外,有些版本還需要同步調整 Configure 相關檔案,實際以你手上版本的目錄結構為準。