title: Generic Makefile Pattern to compile every source to corresponding object file
body: <p>Hi i m trying to use a generic makefile to build my project. </p>
  
  <p>The error i get is that duplicate functions exist in my sourcecode. I checked with nm and yes they are there. I know i could write a normale makefile with every sourcefile named explicitly and compiled line by line. But the nature of my project demands, that others are able to integrate new plattforms without and port the whole affair to new machines. (Which means in the long run, even the direct library paths will have to become relative)
  Any help appreciated.</p>
  
  <p>Additional Information:
  The Host system is windows 7, i compile using MinGW 32 (nuwens distro) - and as crosscompiler and library provider i use gnu-arm-none-eabi-gcc from the ARM-Launchpad.</p>
  
  <pre><code>#PATHS
  
  GEN_SOURCE_PATH=    ../../source/generate_osek
  HW_SOURCE_PATH=     $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/
  ASM_PATH=           $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/$(PLATFORM)_asm
  HEADER_PATH=        $(GEN_SOURCE_PATH)/header
  
  
  #HEADERS
  ORG_HEADERS =   $(HEADER_PATH)/
  HW_HEADERS  =   $(HEADER_PATH)/$(PLATFORM)_head/
  ALL_HEADERS =   $(ORG_HEADERS) $(HW_HEADERS)
  
  INCLUDES =      -I/c/osek/workspace/htosek/source/generate_osek/header/
  INCLUDES +=     -I/c/osek/workspace/htosek/source/generate_osek/header/$(PLATFORM)_head/
  INCLUDES +=     -I/c/osek/workspace/htosek/source/$(PLATFORM)_hw/
  INCLUDES +=     -I/c/osek/workspace/htosek/source/
  INCLUDES +=     -I/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/include/
  #Library
  LIBS = -L/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/  -lgcc
  
  
  #LISTSOURCES
  ASM_INCLUDE= -I$(ASM_PATH)/   
  
  
  #SOURCEFILES
  CFILES      =   $(foreach dir,$(GEN_SOURCE_PATH)/,$(notdir $(wildcard $(dir)/*.c)))
  CFILES      +=  $(foreach dir,$(HW_SOURCE_PATH),$(notdir $(wildcard $(dir)/*.c)))
  SFILES      =   $(foreach dir,$(ASM_PATH)/,$(notdir $(wildcard $(dir)/*.s)))
  HFILES      =   $(foreach dir,$(ORG_HEADERS),$(notdir $(wildcard $(dir)/*.h)))
  HFILES      +=  $(foreach dir,$(HW_HEADERS),$(notdir $(wildcard $(dir)/*.h)))
  
  #VPATH
  VPATH= $(GEN_SOURCE_PATH)
  VPATH+= $(ASM_PATH)
  VPATH+= $(ALL_HEADERS)
  
  #DEPENDENCIES
  DEP = $(subst .o,.d,$(SOBJS) $(COBJS))
  #Converting
  
  SOBJS  =    $(SFILES:.s=.o)
  COBJS   =   $(CFILES:.c=.o)
  OBJECTS =  $(COBJS) $(SOBJS) 
  #WARNINGS= -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror -Wno-unused
  LOCAL_CFLAG= -v -d -c    -ffreestanding
  C_FLAGS += $(LOCAL_CFLAG)
  
  #GENERATE_OSEK BINARY
  os: bin
      @echo "Generating binary image file: $@"
      $(OBJCOPY_DEVICE) -O binary $(EXEC) $@
  
  #LINKING OBJECTS
  bin:$(OBJECTS)  
      @echo ""
      @echo "Linking $&lt;..." 
      $(LD_DEVICE) -o $(EXEC)  $(OBJECTS)  $(LD_FLAGS) $(LIBS)
      @echo ""
      @echo "And done..."
  
  #COMPILING ASSEMBLY
  $(SOBJS):  $(SFILES)
      @echo ""
      @echo "Assembling $&lt;..."
      $(ASM_DEVICE)  $(ASM_INCLUDE)  $(ASM_FLAGS) -g -v  -o $@   $^ 
  
  #COMPILING C SOURCES
  $(COBJS): $(CFILES) ${HFILES}
      @echo ""
      @echo Compiling $&lt;...
      $(CC_DEVICE) $(C_FLAGS)  $(INCLUDES) $(LIBS)  -g  -o $@  $^
  
  os_clean:
       RM *.o 
       RM $(EXEC) 
  </code></pre>
  
  <p>Update:
  I added some missing includes to the VPATH and now at least it compiles to the error stated in the beginning:</p>
  
  <p>Updated Makefile:
      #PATHS</p>
  
  <pre><code>GEN_SOURCE_PATH=    ../../source/generate_osek
  HW_SOURCE_PATH=     $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/
  ASM_PATH=           $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/$(PLATFORM)_asm
  HEADER_PATH=        $(GEN_SOURCE_PATH)/header
  
  
  #HEADERS
  ORG_HEADERS =   $(HEADER_PATH)/
  HW_HEADERS  =   $(HEADER_PATH)/$(PLATFORM)_head/
  ALL_HEADERS =   $(ORG_HEADERS) $(HW_HEADERS)
  
  INCLUDES =      -I/c/osek/workspace/htosek/source/generate_osek/header/
  INCLUDES +=     -I/c/osek/workspace/htosek/source/generate_osek/header/$(PLATFORM)_head/
  INCLUDES +=     -I/c/osek/workspace/htosek/source/
  INCLUDES +=     -I/c/osek/workspace/htosek/source/$(PLATFORM)_hw/
  INCLUDES +=     -I/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/include/
  #Library
  LIBS = -L/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/  -lgcc
  
  
  #LISTSOURCES
  ASM_INCLUDE= -I$(ASM_PATH)/   
  
  
  #SOURCEFILES
  CFILES      =   $(foreach dir,$(GEN_SOURCE_PATH)/,$(notdir $(wildcard $(dir)/*.c)))
  CFILES      +=  $(foreach dir,$(HW_SOURCE_PATH),$(notdir $(wildcard $(dir)/*.c)))
  SFILES      =   $(foreach dir,$(ASM_PATH)/,$(notdir $(wildcard $(dir)/*.s)))
  HFILES      =   $(foreach dir,$(ORG_HEADERS),$(notdir $(wildcard $(dir)/*.h)))
  HFILES      +=  $(foreach dir,$(HW_HEADERS),$(notdir $(wildcard $(dir)/*.h)))
  
  #VPATH
  VPATH= $(GEN_SOURCE_PATH)/
  VPATH += $(HW_SOURCE_PATH)
  VPATH+= $(ASM_PATH)
  VPATH+= $(ALL_HEADERS)
  
  #DEPENDENCIES
  DEP = $(subst .o,.d,$(SOBJS) $(COBJS))
  #Converting
  
  SOBJS  =    $(SFILES:.s=.o)
  COBJS   =   $(CFILES:.c=.o)
  OBJECTS =  $(COBJS) $(SOBJS) 
  #WARNINGS= -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror -Wno-unused
  LOCAL_CFLAG= -v -d -c    -ffreestanding
  C_FLAGS += $(LOCAL_CFLAG)
  
  #GENERATE_OSEK BINARY
  os: bin
      @echo "Generating binary image file: $@"
      $(OBJCOPY_DEVICE) -o binary $(EXEC) $@
  
  #LINKING OBJECTS
  bin:$(OBJECTS)  
      @echo ""
      @echo "Linking $&lt;..." 
      $(LD_DEVICE) -o $(EXEC)  $(OBJECTS)  $(LD_FLAGS) $(LIBS) -o $@
      @echo ""
      @echo "And done..."
  
  #COMPILING ASSEMBLY
  $(SOBJS):  $(SFILES)
      @echo ""
      @echo "Assembling $&lt;..."
      $(ASM_DEVICE)  $(ASM_INCLUDE)  $(ASM_FLAGS) -g -v  -o $@   $^ 
  
  #COMPILING C SOURCES
  $(COBJS): $(CFILES) ${HFILES}
      @echo ""
      @echo "Compiling: $&lt; -&gt; $@"
      $(CC_DEVICE) $(C_FLAGS)  $(INCLUDES) $(LIBS)  -g  -o $@ $^
  
  os_clean:
       RM *.o 
       RM $(EXEC) 
       RM *.gch
  </code></pre>
  
  <p><strong>Updated Error Sample:</strong></p>
  
  <pre><code>debug_hook.o: In function `resetAlarm':
  c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:19: multiple definition of `resetAlarm'
  alarm.o:c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:19: first defined here
  debug_hook.o: In function `RaiseAlarm':
  c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:25: multiple definition of `RaiseAlarm'
  alarm.o:c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:25: first defined here
  debug_hook.o: In function `TimeTillAlarm':
  c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:54: multiple definition of `TimeTillAlarm'
  alarm.o:c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:54: first defined here
  make: *** [bin] Error 1
  </code></pre>
  
  <p>TimeTillAlarm exists definatly just once. No Predeclarations elsewhere. Greped for it over the whole source.</p>
  
  <p>Linker Script:</p>
  
  <p>arm-none-eabi-ld  -o test.elf alarm.o debug_hook.o fifo_array.o      fifo_queue.o htosek_startup.o i_basic_op_abstraction.o     i_driver_op_abstraction.o othsek.o task.o at91sam72s256_restarttask.o     at91sam72s256_startup.o at91sam72s256_system.o __irfunctions.o     _activatetask.o _contextswitch.o _cstartup.o _ir_vector_table.o       _restartTask.o  -M -nostartfiles -nostdlib -L/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/  -lgcc'</p>
  
  <p><strong>Update:</strong>
  nm shows that all assembler o.files have the same symbol</p>
  
  <pre><code>P@P-PC /c/osek/workspace/htosek/project/test
  $ nm debug_hook.o
  00000084 t $a
  00000000 t $a
  00000010 N $d
  00000080 t $d
  0000047c C HTOsek
  00000098 T InitAlarm
  0000000c T RaiseAlarm
  00000000 T resetAlarm
  00000084 T TimeTillAlarm
  
  P@P-PC /c/osek/workspace/htosek/project/test
  $ nm fifo_array.o
  00000084 t $a
  00000000 t $a
  00000010 N $d
  00000080 t $d
  0000047c C HTOsek
  00000098 T InitAlarm
  0000000c T RaiseAlarm
  00000000 T resetAlarm
  00000084 T TimeTillAlarm
  </code></pre>
  
  <p>o.files from c_sources seem to be in the same shape, but have at least a diffrent set of symbols. 
  <strong>Conclusion:</strong> One sourcefile is made into diffent named o.files for both groups. Anyone Suggestions to why?</p>
  
  <p>Thanks again for the patience and advice</p>