乙個STM32專案的makefile模版詳解

2021-07-25 01:25:18 字數 4595 閱讀 6815

啥都不說了,直接上**,注釋相當詳細了。

#stm32 makefile template

# 生成的檔名《專案名》

project = stm32_mini_led

# 定義檔案格式和檔名

target

:= $(

project)

target_elf

:= $(

target).elf

target_bin

:= $(

target).bin

target_hex

:= $(

target).hex

objcpflags_elf_to_bin = -obinary

objcpflags_elf_to_hex = -oihex

objcpflags_bin_to_hex = -ibinary -oihex

# 定義路徑

top_dir = .

script_dir

:= $(

top_dir)/scripts

startup_dir

:= $(

top_dir)/third_party/libraries/cmsis/cm3/devicesupport/st/stm32f10x/startup

inc_dir

:= -i

$(top_dir)/src -i

$(top_dir)/src/driver -i

$(top_dir)/src/edp -i

$(top_dir)/src/network

# 設定shell環境變數

#export ld_library_path = $ld_library_path:/users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/

ldscript

:= $(

script_dir)/stm32_flash.ld

# 定義編譯工具

cc = arm-none-eabi-gcc

as = arm-none-eabi-as

ld = arm-none-eabi-ld

ar = arm-none-eabi-ar

objcp = arm-none-eabi-objcopy

# 定義編譯標誌

ccflags += -wall -mcpu=cortex-m3 -mthumb -g -mfloat-abi=soft -march=armv7-m

asflags += -wall -mcpu=cortex-m3 -mthumb

ldflags += -t

$(ldscript) #-a armv7-m

ldflags += -l /users/ch-yanghl/gcc-arm-none-eabi/lib/gcc/arm-none-eabi/5.4.1

ldflags += -l /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb

# 要鏈結的靜態庫

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libc.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libg.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libm.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/lib/gcc/arm-none-eabi/5.4.1/libgcc.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libnosys.a

# .c檔案中的標頭檔案引用查詢路徑

ccflags += $(

inc_dir)

# .s檔案的flags

#asflags +=

# .c檔案編譯時定義巨集

ccflags += -d

stm32f10x_md -d

use_stdperiph_driver

# 新增啟動檔案

#source += $(script_dir)/startup_stm32f10x_md.c

source_asm += $(

startup_dir)/gcc_ride7/startup_stm32f10x_md.s

# 展開工作 子目錄中的inc檔案(inc檔案中新增需要編譯鏈結的.c,.s等檔案)

-include

$(top_dir)/src/make.inc

-include

$(top_dir)/third_party/make.inc

# 替換檔案字尾

c_objs

:= $(

source

:%.c=%.o)

asm_objs

:= $(

source_asm

:%.s=%.o)

# 編譯命令的定義

compile = $(

cc) $(

ccflags) -c $< -o $@

assemble = $(

as) $(

asflags) -c $< -o $@

link = $(

ld) $+

$(ldflags) $(

ldlibs) -o $@

elf_to_bin = $(

objcp) $(

objcpflags_elf_to_bin) $<

$@bin_to_hex = $(

objcp) $(

objcpflags_bin_to_hex) $<

$@# 定義偽目標

.phony

: all clean printf

# 定義規則

all:

$(target_hex)

@echo

"build done"

$(target_hex):

$(target_bin)

$(bin_to_hex)

$(target_bin):

$(target_elf)

$(elf_to_bin)

$(target_elf):

$(c_objs) $(

asm_objs)

$(link)

$(c_objs):%.o:%.c

$(compile)

$(asm_objs):%.o:%.s

$(assemble)

printf:

@echo

$(asm_objs)

@echo

$(assemble)

# 清理項

clean:

rm -f $(

target_hex)

rm -f $(

target_bin)

rm -f $(

target_elf)

rm -f $(

c_objs) $(

asm_objs)

@echo

"clean done"

再詳細說一下注釋中沒有的可能有疑問的地方:

# 要鏈結的靜態庫

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libc.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libg.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libm.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/lib/gcc/arm-none-eabi/5.4.1/libgcc.a

#ldlibs += /users/ch-yanghl/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/libnosys.a

-這一塊為什麼要注釋:因為文中使用了

ldscript := $(script_dir)/stm32_flash.ld

/*stm32_flash.ld檔案中最後部分內容*/

/discard/ :

這個ld指令碼檔案,該檔案中已經包含了這些庫。

STM32簡單彙編專案的建立

1.新建工程 2.選擇配置 選擇晶元型號,這裡我的晶元是f103c8t6 選擇啟動檔案與核心 3.新增彙編檔案 右鍵source group新建彙編檔案 複製下列 area mydata,data area mycode,code entry export main main mov r0,10 m...

STM32空白專案的搭建和燒錄

環境的重要性 安裝環境是開發者為開發產品的第一步,其過程是繁瑣枯燥的。長時間地專注於開發專案以及快速上手帶來的快樂會使我們忽視學習配置開發環境的重要性,但其實理解並記憶配置自己的環境會給我們的開發來帶更多的便利和意想不到的創造力。為了便於理解記憶,以下我以我建立的工程的目錄檔案樹做出解釋。上圖中,l...

建立第乙個stm32專案工程常見錯誤警告

objects lightoneled.axf error l6218e undefined symbol systeminit referred from startup stm32f10x hd.o 意思是systeminit這個函式未定義 在startup stm32f10x hd.o引用 在...