Problem with own Makefile and arduino mega 2560

Hello all!

It’s my first time with arduino, and I’ve got some problem. I’ve programmed some avrs already, and first what I have done when I got arduino to my hands was to write my own Makefile.
And, after some battles, it’s compilling ok. But it’s not working :frowning:

When I compile simple program, that blinks all (or just some) ports, it’s working fine, when I do that with arduino IDE, but when I use my makefile and avrdude, it’s not.
I think avr is still restarting, because L-diode on arduino board is blinking irregullar, where with arduino IDE, it only blinks once, after restart.

I use arduino mega 2560.

And here’s my Makefile.

TARGET	=	test
MCU	=	atmega2560
F_CPU	=	16000000UL
# LIBS	=	ITDB02

BASE	= ../deps
ARDUINO = $(BASE)/arduino
LIBDIR  = $(BASE)/libraries
INCLUDES = -I$(ARDUINO)/cores/arduino -I$(LIBDIR) $(LIBS:%=-I$(LIBDIR)/%)
PORT	= 	/dev/ttyACM0

CC	=	avr-gcc
CXX	=	avr-g++

OBJCOPY = avr-objcopy

UPLOAD_RATE = 115200
# AVRDUDE = $(BASE)/tools/avrdude
AVRDUDE = avrdude
AVRDUDE_PROGRAMMER = stk500v2
AVRDUDE_PORT = $(PORT)
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -b $(UPLOAD_RATE)

# -C $(BASE)/tools/avrdude.conf

ALLFLAGS= -Os -Wall \
	  -mmcu=$(MCU) -DF_CPU=$(F_CPU) \
	  -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
	  -ffunction-sections
CFLAGS	= $(INCLUDES) $(ALLFLAGS) 
LDFLAGS	= $(ALLFLAGS) -Wl,-gc-sections
FORMAT 	= ihex

ALIBS	= $(LIBS:%=$(LIBDIR)/%)

SRC 	= $(wildcard src/*.c) \
	  $(wildcard $(ARDUINO)/cores/arduino/*.c) \
	  $(wildcard $(ALIBS:%=%/*.c))
	  
CXXSRC	= $(wildcard src/*.cpp) \
	  $(wildcard $(ARDUINO)/cores/arduino/*.cpp) \
	  $(wildcard $(ALIBS:%=%/*.cpp))
	  
OBJS 	= $(SRC:.c=.c.o) $(CXXSRC:.cpp=.cpp.o)


all: build


build: $(TARGET).elf $(TARGET).hex

program: upload
upload: $(TARGET).hex
	$(BASE)/pulsedtr.py $(PORT)
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)


$(TARGET).elf: $(OBJS)
	$(CC) $(LDFLAGS) -o $(TARGET).elf $(OBJS)

$(TARGET).hex: $(TARGET).elf
	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@

$(TARGET).eep: $(TARGET).elf
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@


%.cpp.o: %.cpp
	$(CXX) $(CFLAGS) -c -o $@ $<

%.c.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -fv $(OBJS)
	rm -fv $(TARGET).*

Well, problem is partially solved. Now it's working fine. I've removed HardwareSerial.cpp from being compiled, and everything looks fine.

Did anyone knows, why?