|
本人AVR新手,更是gcc-avr新手,学到makefile,一则为了加深印象,二则可以供些新手参考,才总结了这个帖子
参考帖子:
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=897204&bbs_page_no=1&search_mode=4&search_text=ChrisChan&bbs_id=9999
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=1639611&bbs_page_no=1&search_mode=4&search_text=ChrisChan&bbs_id=9999
参考文件:
makefile文件的规则和语法ourdev_678110GWM8EJ.pdf(文件大小:1.34M) (原文件名:makefile文件的规则和语法.pdf)
AVR单片机GCC程序设计ourdev_678111F1UGZE.pdf(文件大小:1.61M) (原文件名:AVR单片机GCC程序设计.pdf)
#-----------------------------MAKE命令参数----------------------------------
#PN的选项中可以去配置这些命令,我们经常用到的就是"make clean","make all"
# make all = Make software.
#
# make clean = Clean out built project files.
#
# make coff = Convert ELF to AVR COFF.
#
# make extcoff = Convert ELF to AVR Extended COFF.
#
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#
# make debug = Start either simulavr or avarice as specified for debugging,
# with avr-gdb or avr-insight as the front end for debugging.
#
# make filename.s = Just compile filename.c into the assembler code only.
#
# make filename.i = Create a preprocessed source file for use in submitting
# bug reports to the GCC project.
#---------------------------------------------------------------------------
#--------------------- 用户定置选项(一般修改的地方)-----------------------
#MCU型号
#CPU频率
#生成烧录文件的格式
#最终目标文件名(除中间目标文件.o和汇编列表文件.lst以外的所有文件以此命名)
#目标文件存放的文件夹,当前目录以"."表示,不要留空
#C源文件,可以是多个C源文件
#C++源文件,可以是多个C++源文件
#汇编源文件,可以是多个汇编源文件(扩展名是大写.S)
#编译优化级别
#调试文件格式,默认是dwarf-2(支持coff),想要支持extcoff请设为stabs
#头文件的其它搜寻路径,多路径空格隔开
#C语言编译标准,可以是C89、gnu89、C99、gnu99(默认)
#C源文件的-D、-U编译选项设置 ******一般不作更改(不用管)******
#汇编源文件的-D、-U编译选项设置 ******一般不作更改(不用管)******
#C++源文件的-D、-U编译选项设置 ******一般不作更改(不用管)******
MCU = atmega16
F_CPU = 8000000
FORMAT = ihex
TARGET = Test
OBJDIR = .
SRC = $(TARGET).c
CPPSRC =
ASRC =
OPT = s
DEBUG = dwarf-2
EXTRAINCDIRS =
CSTANDARD = -std=gnu99
CDEFS = -DF_CPU=$(F_CPU)UL
ADEFS = -DF_CPU=$(F_CPU)
CPPDEFS = -DF_CPU=$(F_CPU)UL
#以上都是定义变量并赋值,供后面调用。
#----------------------------- 编译选项 ------------------------------------
#---------------- C语言文件编译选项 --------------------
#通过赋值和追加的方式,将编译选项汇总给CFLAGS,供后面调用
CFLAGS = -g$(DEBUG)
CFLAGS += $(CDEFS)
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
#CFLAGS += -mshort-calls
#CFLAGS += -fno-unit-at-a-time
#CFLAGS += -Wundef
#CFLAGS += -Wunreachable-code
#CFLAGS += -Wsign-compare
#工程中的.lst文件在此生成
CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += $(CSTANDARD)
#---------------- C++语言文件编译选项 ------------------
#通过赋值和追加的方式,将编译选项汇总给CPPFLAGS,供后面调用
CPPFLAGS = -g$(DEBUG)
CPPFLAGS += $(CPPDEFS)
CPPFLAGS += -O$(OPT)
CPPFLAGS += -funsigned-char
CPPFLAGS += -funsigned-bitfields
CPPFLAGS += -fpack-struct
CPPFLAGS += -fshort-enums
CPPFLAGS += -fno-exceptions
CPPFLAGS += -Wall
CPPFLAGS += -Wundef
#CPPFLAGS += -mshort-calls
#CPPFLAGS += -fno-unit-at-a-time
#CPPFLAGS += -Wstrict-prototypes
#CPPFLAGS += -Wunreachable-code
#CPPFLAGS += -Wsign-compare
CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
#CPPFLAGS += $(CSTANDARD)
#---------------- 汇编语言文件编译选项 -----------------
#通过赋值和追加的方式,将编译选项汇总给ASFLAGS,供后面调用
ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
#------------------------------- 库选项 ------------------------------------
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
#printf库版本选择,留空的话为printf标准库
PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
#scanf库版本选择,留空的话为scanf标准库
SCANF_LIB =
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)
#数学库选项
MATH_LIB = -lm
#函数库的其它搜寻路径,多路径空格隔开
EXTRALIBDIRS =
#--------------------------- 外扩存储器选项 --------------------------------
#在内部RAM地址后外扩64K外部RAM,用作.data段、.bss段、heap(堆区/动态内存分配)
#如下定义.data段(0x800000)从0x1100 开始,注意是外部RAM
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
#在内部RAM地址后外扩64K外部RAM,只用作heap(堆区/动态内存分配)
#也就是用堆的方式使用外部RAM
#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
#默认不外扩RAM,留空
EXTMEMOPTS =
#------------------------------ 链接选项 -----------------------------------
#可以看出链接是操作库、外部存储和中间目标文件
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(EXTMEMOPTS)
LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#LDFLAGS += -T linker_script.x
#------------------------- 编程下载选项(avrdude) ---------------------------
#一般不用,用得话也比较简单
AVRDUDE_PROGRAMMER = stk500v2
AVRDUDE_PORT = com1 # programmer connected to serial device
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
#AVRDUDE_ERASE_COUNTER = -y
#AVRDUDE_NO_VERIFY = -V
#AVRDUDE_VERBOSE = -v -v
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
#------------------------------ 调试选项 -----------------------------------
#WinAVR的调试选项,一般用不着
DEBUG_MFREQ = $(F_CPU)
# DEBUG_UI = gdb
DEBUG_UI = insight
DEBUG_BACKEND = avarice
#DEBUG_BACKEND = simulavr
GDBINIT_FILE = __avr_gdbinit
JTAG_DEV = /dev/com1
DEBUG_PORT = 4242
DEBUG_HOST = localhost
#============================================================================
#以上只是定义了要用到的选项,下面才开始调用,也就是真正地执行命令
#当然,先做一下准备工作,包括:定义(等价)一些命令、显示信息和以上选项的汇总
#定义一些等价命令,便于移植(不同平台只需对“=”后面作下更改)
SHELL = sh
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
AR = avr-ar rcs
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -f
REMOVEDIR = rm -rf
COPY = cp
WINSHELL = cmd
#定义一些过程中要显示的信息
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_COFF = Converting to AVR COFF:
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling C:
MSG_COMPILING_CPP = Compiling C++:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:
MSG_CREATING_LIBRARY = Creating library:
#定义所有的中间目标文件和列表文件
OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
#生成依赖文件的编译选项,工程中的.d文件在此生成
GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
#将上面定义的编译选项和MCU型号汇总到“ALL_CFLAGS”
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
#--------------- 以下就是makefile真正执行命令的地方了 ----------------------
#“MAKE ALL” 就是直接执行下面这条指令了,
#依次执行“begin-gccversion-sizebefore-build-sizeafter-end”这几个命令,具体命令在下面
all: begin gccversion sizebefore build sizeafter end
#在“all”里面被执行,选择要生成哪些最终目标文件,如:加入coff和extcoff
#注:elf即便不在下面列表,因其它文件依赖它所以elf也会生成
build: elf hex eep lss sym
#build: lib
elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss
sym: $(TARGET).sym
LIBNAME=lib$(TARGET).a
lib: $(LIBNAME)
#begin和end中命令
begin:
@echo
@echo $(MSG_BEGIN)
end:
@echo $(MSG_END)
@echo
#hex和elf文件大小的定义
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
#显示前后elf文件大小命令
sizebefore:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
2>/dev/null; echo; fi
sizeafter:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
2>/dev/null; echo; fi
#编译器版本显示命令
gccversion :
@$(CC) --version
#编程下载命令
program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
#调试配置命令
gdb-config:
@$(REMOVE) $(GDBINIT_FILE)
@echo define reset >> $(GDBINIT_FILE)
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
@echo end >> $(GDBINIT_FILE)
@echo file $(TARGET).elf >> $(GDBINIT_FILE)
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
ifeq ($(DEBUG_BACKEND),simulavr)
@echo load >> $(GDBINIT_FILE)
endif
@echo break main >> $(GDBINIT_FILE)
#调试配置命令
debug: gdb-config $(TARGET).elf
ifeq ($(DEBUG_BACKEND), avarice)
@echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
@$(WINSHELL) /c pause
else
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
$(DEBUG_MFREQ) --port $(DEBUG_PORT)
endif
@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
#定义将elf转化成coff的命令,供下面具体转化命令调用
COFFCONVERT = $(OBJCOPY) --debugging
COFFCONVERT += --change-section-address .data-0x800000
COFFCONVERT += --change-section-address .bss-0x800000
COFFCONVERT += --change-section-address .noinit-0x800000
COFFCONVERT += --change-section-address .eeprom-0x810000
#用elf生成coff,工程中的.coff文件在此生成
coff: $(TARGET).elf
@echo
@echo $(MSG_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
#用elf生成coff,工程中的extcoff文件在此生成
extcoff: $(TARGET).elf
@echo
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
#用elf生成hex,工程中的.hex文件在此生成
%.hex: %.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock $< $@
#用elf生成eep,工程中的.eep文件在此生成
%.eep: %.elf
@echo
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
#用elf生成lss,工程中的.lss文件(扩展列表)在此生成
%.lss: %.elf
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S -z $< > $@
#用elf生成sym,工程中的.sym文件(符号表)在此生成
%.sym: %.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
#通过.o(中间目标文件)生成.a(库)文件,工程中的.a文件在此生成
.SECONDARY : $(TARGET).a
.PRECIOUS : $(OBJ)
%.a: $(OBJ)
@echo
@echo $(MSG_CREATING_LIBRARY) $@
$(AR) $@ $(OBJ)
#链接:通过.o(中间目标文件)生成elf文件,工程中的.elf文件在此生成
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
@echo
@echo $(MSG_LINKING) $@
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
#编译:通过C源文件生成.o(中间目标文件),工程中的.o文件在此生成
$(OBJDIR)/%.o : %.c
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $@
#编译:通过C++源文件生成.o(中间目标文件),工程中的.o文件在此生成
$(OBJDIR)/%.o : %.cpp
@echo
@echo $(MSG_COMPILING_CPP) $<
$(CC) -c $(ALL_CPPFLAGS) $< -o $@
#编译:通过C源文件生成.s(汇编文件)
%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $@
#编译:通过C++源文件生成.s(汇编文件)
%.s : %.cpp
$(CC) -S $(ALL_CPPFLAGS) $< -o $@
#汇编: 通过.s(汇编文件)源文件生成.o(中间目标文件)
$(OBJDIR)/%.o : %.S
@echo
@echo $(MSG_ASSEMBLING) $<
$(CC) -c $(ALL_ASFLAGS) $< -o $@
#通过C源文件生成.i(预处理源),用来发送一些错误报告
%.i : %.c
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
#“MAKE CLEAN” 就是直接执行下面这条指令了,清除除源文件以外的所有文件
#依次执行“begin-clean_list-end”这三条命令,具体在下面
clean: begin clean_list end
clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).eep
$(REMOVE) $(TARGET).cof
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).sym
$(REMOVE) $(TARGET).lss
$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
$(REMOVE) $(SRC:.c=.s)
$(REMOVE) $(SRC:.c=.d)
$(REMOVE) $(SRC:.c=.i)
$(REMOVEDIR) .dep
#创建中间目标文件目录
$(shell mkdir $(OBJDIR) 2>/dev/null)
#包含依赖文件
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
#定义这些伪目标, 避免Makefile中定义的只执行命令的目标和实际文件出现名字冲突
# 提高执行make的效率
.PHONY : all begin finish end sizebefore sizeafter gccversion \
build elf hex eep lss sym coff extcoff \
clean clean_list program debug gdb-config |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|