|
用了很多年winavr,makefile就是用Mfile生成的那个,这个makefile有个不爽的地方就是生成的编译文件都在当前目录,源文件和编译文件在一起,搞的比较乱,不像iar或者keil,编译文件都在一个独立的目录里,比较清爽。
最近看fatfs,发现里边的makefile写的很好,实现了obj文件在一个独立目录的功能,我将makefile稍加优化一下,特此推荐。献给喜欢用winavr的朋友。
### Project name (also used for output file name)
PROJECT = test
### Source files and search directory
CSRC = main.c adc.c usart.c
ASRC =
VPATH =
### Target device
DEVICE = atmega88
### Optimization level (0, 1, 2, 3 or s)
OPTIMIZE = s
### C Standard level (c89, gnu89, c99 or gnu99)
CSTD = c99
### Include dirs, library dirs and definitions
LIBS =
LIBDIRS =
INCDIRS =
EFS = F_CPU=8000000
ADEFS = $(DEFS)
### Warning contorls
WARNINGS = all extra
### Output directory
OBJDIR = obj
### Output hex file format and debugger type
HEXFMT = ihex
DEBUG = dwarf-2
### Programs to build porject
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
# Define all object files
COBJ = $(CSRC:.c=.o)
AOBJ = $(ASRC:.S=.o)
COBJ := $(addprefix $(OBJDIR)/,$(COBJ))
AOBJ := $(addprefix $(OBJDIR)/,$(AOBJ))
PROJECT := $(OBJDIR)/$(PROJECT)
# Flags for C files
FLAGS += -std=$(CSTD)
CFLAGS += -g$(DEBUG)
CFLAGS += -mmcu=$(DEVICE)
CFLAGS += -O$(OPTIMIZE) -mcall-prologues
CFLAGS += $(addprefix -W,$(WARNINGS))
CFLAGS += $(addprefix -I,$(INCDIRS))
CFLAGS += $(addprefix -D,$(DEFS))
CFLAGS += -Wp,-M,-MP,-MT,$(OBJDIR)/$(*F).o,-MF,$(OBJDIR)/$(*F).d
# Assembler flags
ASFLAGS += $(addprefix -D,$(ADEFS)) -Wa,-gstabs,-g$(DEBUG)
ALL_ASFLAGS = -mmcu=$(DEVICE) -I. -x assembler-with-cpp $(ASFLAGS)
# Linker flags
LDFLAGS += -Wl,-Map,$(PROJECT).map
BEGIN = @echo -------- begin --------
END = @echo -------- end --------
FINISH = @echo Errors: none
begin:
$(BEGIN)
end:
$(END)
finished:
$(FINISH)
# Display size of file.
#HEXSIZE = $(SIZE) -C --mcu=$(DEVICE) $(PROJECT).elf
HEXSIZE = $(SIZE) --mcu=$(DEVICE) $(PROJECT).elf
# Display size of file.
sizebefore:
@echo Size before:
-$(HEXSIZE)
sizeafter:
@echo Size after:
$(HEXSIZE)
# Default target.
all: begin sizebefore build sizeafter finished end
build: elf hex lss
hex: $(PROJECT).hex
bin: $(PROJECT).bin
elf: $(PROJECT).elf
lst: $(PROJECT).lst
lss: $(PROJECT).lss
sym: $(PROJECT).sym
# Display compiler version information.
version :
@$(CC) --version
# Create final output file from ELF output file.
%.hex: %.elf
# @echo
$(OBJCOPY) -j .text -j .data -j .eeprom -j .fuse -O $(HEXFMT) $< $@
%.bin: %.elf
# @echo
$(OBJCOPY) -j .text -j .data -O binary $< $@
# Create extended listing file from ELF output file.
%.lst: %.elf
# @echo
$(OBJDUMP) -h -S -C $< > $@
# Create extended listing file from ELF output file.
%.lss: %.elf
$(OBJDUMP) -h -S $< > $@
# Create a symbol table from ELF output file.
%.sym: %.elf
@echo
$(NM) -n $< > $@
# Link: create ELF output file from object files.
%.elf: $(AOBJ) $(COBJ)
# @echo
@echo Linking...
$(CC) $(CFLAGS) $(AOBJ) $(COBJ) --output $@
# Compile: create object files from C source files. ARM or Thumb(-2)
$(COBJ) : $(OBJDIR)/%.o : %.c
# @echo
# @echo $< :
$(CC) -c $(CFLAGS) $< -o $@
# Assemble: create object files from assembler source files. ARM or Thumb(-2)
$(AOBJ) : $(OBJDIR)/%.o : %.S
# @echo
# @echo $< :
$(CC) -c $(ALL_ASFLAGS) $< -o $@
# Target: clean project.
clean:
@echo
@echo -------- begin clean --------
rm -f -r $(OBJDIR) | exit 0
@echo -------- clean obj ok --------
# Include the dependency files.
-include $(shell mkdir $(OBJDIR) 2>/dev/null) $(wildcard $(OBJDIR)/*.d)
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|