Header dependencies in Makefile: offline toolchain does not rebuld the mBed project when only the header file changes

10 Oct 2012

Hi,

I export my mBed project and use an offline toolcahin (Code Sourcery and Eclipse) to edit and compile my project. Works well.

My project has a main file (main.cpp) and a serial communication module that is defined and implemented in a COMfunc.h file.

However,calling <b>make<\b> does not rebuild the project unless I change the main.cpp file. In another words, when I change the COMfunc.h file only, the makefile does not detect the file changes and not rebuild the project.

I think this is due to the header file dependency which has been disscussed in general GCC makefile. See http://scottmcpeak.com/autodepend/autodepend.html and http://stackoverflow.com/questions/2394609/makefile-header-dependencies

I update the makefile a bit as suggested by http://stackoverflow.com/questions/2394609/makefile-header-dependencies

But it still not work. I am not familiar with the makefile, any suggestions are greatly appreciated.

The part of the makefile I have modified is:

# I add the depend. as suggested by 
# http://stackoverflow.com/questions/2394609/makefile-header-dependencies
#
SRC=./COMfunc.h
depend: .depend
.depend: $(SRC)
	rm -f ./.depend

all: $(PROJECT).bin

clean:
	rm -f $(PROJECT).bin $(PROJECT).elf $(OBJECTS)

.s.o:
	$(AS)  $(CC_FLAGS) $(CC_SYMBOLS) -o $@ $<

.c.o:
	$(CC)  $(CC_FLAGS) $(CC_SYMBOLS) $(ONLY_C_FLAGS)   $(INCLUDE_PATHS) -o $@ $<
	$(CC)  $(CC_FLAGS) $(CC_SYMBOLS) $(ONLY_C_FLAGS)   $(INCLUDE_PATHS) -MM -> $@ ./.depend
	include .depend
	
.cpp.o:
	$(CPP) $(CC_FLAGS) $(CC_SYMBOLS) $(ONLY_CPP_FLAGS) $(INCLUDE_PATHS) -o $@ $<


$(PROJECT).elf: $(OBJECTS) $(SYS_OBJECTS)
	$(LD) $(LD_FLAGS) -T$(LINKER_SCRIPT) $(LIBRARY_PATHS) -o $@ $^ $(LIBRARIES) $(LD_SYS_LIBS) $(LIBRARIES) $(LD_SYS_LIBS)

# copy the binary output *.elf to mBed MBEDDRIVE\*.bin
# so that no manually copy *.bin to mBed is required. 
# Press 'RESET' at mBed to start executing the program
$(PROJECT).bin: $(PROJECT).elf
	$(OBJCOPY) -O binary $< $(MBEDDRIVE)\$@

11 Oct 2012

I updated my gcc4mbed project a few months ago to do just this. We should be able to make similar changes to the export featured created Makefile. I just took the basic HelloWorld sample from the online compiler, exported it to Code Sourcery, and then modified the Makefile to create header dependencies.

Here is my modified version of that Makefile (my updates have comments containing AdamGr ):

# This file was automagically generated by mbed.org. For more information, 
# see http://mbed.org/handbook/Exporting-to-CodeSourcery

GCC_BIN = 
PROJECT = HelloWorld
OBJECTS = ./main.o 
SYS_OBJECTS = ./mbed/LPC1768/GCC_CS/sys.o ./mbed/LPC1768/GCC_CS/cmsis_nvic.o ./mbed/LPC1768/GCC_CS/system_LPC17xx.o ./mbed/LPC1768/GCC_CS/core_cm3.o ./mbed/LPC1768/GCC_CS/startup_LPC17xx.o 
INCLUDE_PATHS = -I. -I./mbed -I./mbed/LPC1768 -I./mbed/LPC1768/GCC_CS 
LIBRARY_PATHS = -L./mbed/LPC1768/GCC_CS 
LIBRARIES = -lmbed -lcapi 
LINKER_SCRIPT = ./mbed/LPC1768/GCC_CS/LPC1768.ld

# AdamGr - For each object file, create a header dependency file.
DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))

############################################################################### 
CC = $(GCC_BIN)arm-none-eabi-gcc
CPP = $(GCC_BIN)arm-none-eabi-g++
CC_FLAGS = -c -Os -fno-common -fmessage-length=0 -Wall -fno-exceptions -mcpu=cortex-m3 -mthumb -ffunction-sections -fdata-sections 

# AdamGr - Compiler flags used to enable creation of header dependencies.
CC_FLAGS += -MMD -MP

ONLY_C_FLAGS = -std=gnu99
ONLY_CPP_FLAGS = -std=gnu++98
CC_SYMBOLS = -DTARGET_LPC1768 -DTOOLCHAIN_GCC_CS -DNDEBUG


AS = $(GCC_BIN)arm-none-eabi-as

LD = $(GCC_BIN)arm-none-eabi-gcc
LD_FLAGS = -mcpu=cortex-m3 -mthumb -Wl,--gc-sections
LD_SYS_LIBS = -lstdc++ -lsupc++ -lm -lc -lgcc

OBJCOPY = $(GCC_BIN)arm-none-eabi-objcopy

all: $(PROJECT).bin

# AdamGr - Clean out the *.d dependency files as well.
clean:
	rm -f $(PROJECT).bin $(PROJECT).elf $(OBJECTS) $(DEPFILES)

.s.o:
	$(AS)  $(CC_FLAGS) $(CC_SYMBOLS) -o $@ $<

.c.o:
	$(CC)  $(CC_FLAGS) $(CC_SYMBOLS) $(ONLY_C_FLAGS)   $(INCLUDE_PATHS) -o $@ $<

.cpp.o:
	$(CPP) $(CC_FLAGS) $(CC_SYMBOLS) $(ONLY_CPP_FLAGS) $(INCLUDE_PATHS) -o $@ $<


$(PROJECT).elf: $(OBJECTS) $(SYS_OBJECTS)
	$(LD) $(LD_FLAGS) -T$(LINKER_SCRIPT) $(LIBRARY_PATHS) -o $@ $^ $(LIBRARIES) $(LD_SYS_LIBS) $(LIBRARIES) $(LD_SYS_LIBS)

$(PROJECT).bin: $(PROJECT).elf
	$(OBJCOPY) -O binary $< $@

# AdamGr - Now include the header dependency files...if they exist.
-include $(DEPFILES)

Hope that helps,

Adam