在把 U-Boot 移植到 ARM920T 平台、並開啟 NAND 相關命令(例如加上 CFG_CMD_NAND)之後,編譯常會停在 undefined reference to 'raise'。這個錯誤的本質並不是缺了某個函數實現,而是連結器沒找到 C 庫與 libgcc。
raise 屬於標準 C 庫;交叉工具列的 libc 若沒有被正確加入連結,就會出現該符號未定義。同時,某些版本還需要調整浮點與對齊相關的編譯選項。
編輯 cpu/arm920t/config.mk,補上必要的選項:
PLATFORM_RELFLAGS += -fno-strict-aliasing -fno-common -ffixed-r8 -msoft-float PLATFORM_CPPFLAGS += $(call cc-option,-D__KERNEL__) PLATFORM_RELFLAGS += $(call cc-option,-msoft-float)
在 U-Boot 根目錄的 Makefile 中,把 PLATFORM_LIBS 改為:
PLATFORM_LIBS += -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc -lc \
-L/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/usr/lib
第一行讓連結器自動找到交叉工具列的 libgcc 與 libc,第二行則明確指向自己工具列的 libc 目錄,路徑請依實際安裝位置調整。
make distclean make open24x0_config # 依實際板型替換 make
執行 make distclean 是必要的:先前失敗留下的中間檔與配置殘留,往往才是「改完還是報錯」的真正原因。
遇到友善之臂提供的 U-Boot 1.1.6 在開啟 NAND 支援後報此錯,對照官方可正常編譯的版本逐項比對,差異就集中在上述兩處:編譯選項與連結庫路徑。釐清「缺符號其實是缺庫」這層因果,比盲目搜尋符號名稱有效得多。
Redis 編譯報 undefined reference to `__sync_add_and_fetch_4 的 32 位元陷阱
解決 undefined reference to png_create_write_struct 問題
undefined reference to 錯誤的原因與解法整理
一步一步學 Linux C:解決 undefined reference to 'pthread_create'
Linux 編譯遇到 undefined reference to 'pthread_create' 的解法
解決 undefined reference to 連結錯誤