makefile入門手記【1】
makefile的重要性
(1)makefile是一項通用技能,我平時用MDK編輯環境程式設計,基本上沒有接觸過makefile。但是仍然曉得makefile的重要性,因為我平時找到的一些很好的開源原始碼例子都是用makefile編譯的,如果我想要了解這些優秀的原始碼,那麼比如要會makefile。
(2)makefile看起來很高階,這個也是個重要的作用。儘管事實上比MDK容易犯錯,也更加不方便。
makefile的歷史淵源
根據Makefile可見,makefile來源於UNIX
分析一個比較簡單的makefile
include ../../py/mkenv.mk
# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h# include py core make definitions
include $(TOP)/py/py.mkCROSS_COMPILE = arm-none-eabi-
INC += -I.
INC += -I$(TOP)
INC += -I$(BUILD)CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT)#Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -ggdb
else
CFLAGS += -Os -DNDEBUG
endifLDFLAGS = -nostdlib -T stm32f405.ld [email protected] --cref
LIBS =SRC_C = \
main.c \
# printf.c \
string0.c \
malloc0.c \
gccollect.c \SRC_S = \
# startup_stm32f40xx.s \
gchelper.s \OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
all: $(BUILD)/firmware.elf
$(BUILD)/firmware.elf: $(OBJ)
$(ECHO) "LINK [email protected]"
$(Q)$(LD) $(LDFLAGS) -o [email protected] $^ $(LIBS)
$(Q)$(SIZE) [email protected]include $(TOP)/py/mkrules.mk
上面這個makefile來自某版本的micropython\ports\bare-arm的路徑下的makefile檔案。