How to make object file into a directory - makefile

Hi using the below make file how can I create all the object files in to a directory obj.
May be this question is very simple but no idea how to do it ?
Makefile
# Makefile
#### variables
RM= rm -vf
CXX = g++
CXXFLAGS = -Wall -g
CPPFLAGS = -I/usr/include/opencv -I/usr/include/opencv2
LDLIBS = -pthread -lbluetooth -lopencv_core -lopencv_imgproc -lopencv_highgui \
-lopencv_ml -lopencv_video -lopencv_features2d \
-lopencv_calib3d -lopencv_objdetect -lopencv_contrib \
-lopencv_legacy -lv4l1 -lv4l2 -lv4lconvert
SOURCEFILES = main.cpp \
bluetooth.cpp \
gpio.cpp \
wifi.cpp \
capturAndSend.cpp \
OBJECTFILES = $(patsubst %.cpp,%.o,$(SOURCEFILES))
PROGNAME= server
### rules
.PHONY: all clean
all: $(PROGNAME)
$(PROGNAME): $(OBJECTFILES)
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $#
clean:
$(RM) $(OBJECTFILES) $(PROGNAME)

There are lots and lots of answers to this question available already. Did you look?
Try:
OBJECTFILES = $(patsubst %.cpp,obj/%.o,$(SOURCEFILES))
...
obj/%.o : %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $# $<

Related

ERROR: rm: cannot remove 'kernel.img': No such file or directory

I've been trying to get my Raspberry Pi 4 OS (not Linux or anything, I'm making an OS from scratch) to work.
The Makefile has this error when I use the command "make":
rm -rf objects
rm -rf SuperPiOS.elf
rm SuperPiOS.img
rm: cannot remove 'SuperPiOS.img': No such file or directory
make: *** [Makefile:110: clean] Error 1
I can't figure out why it wouldn't work though.
Here's the Makefile:
CFLAGS= -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd
CXXFLAGS= -ggdb3 -O0 -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a72+nosimd
CSRCFLAGS= -O2 -Wall -Wextra
LFLAGS= -ffreestanding -O2 -nostdlib
IMG_PATH= ../
CFILES= $(wildcard *.c)
OFILES= $(CFILES:.c=.o)
GCCFLAGS= -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
GCCPATH= gcc-arm-10.2-2020.11-x86_64-aarch64-none-elf
GCCPATHAARCH= $(GCCPATH)/aarch64-none-elf/bin
GCCPATHBIN= $(GCCPATH)/bin
ASMCFLAGS= -f elf32 -F dwarf -g -w+all
ASM= -s
# Location of the files
KER_SRC = ../src/kernel
KER_MENU_SRC = ../src/kernel/menu
KER_HEAD = ../include
COMMON_SRC = ../src/common
UI_IMAGES = ../images/ui
SPE_GAMES = ../spe_games
DINOBYTE = $(SPE_GAMES)/dinobyte
OBJ_DIR = objects
ASMSOURCES = $(wildcard $(KER_SRC)/*.s)
KERSOURCES = $(wildcard $(KER_SRC)/*.c)
#KERSOURCES = $(wildcard $(KER_SRC)/$(ARCHDIR)/*.c)
COMMONSOURCES = $(wildcard $(COMMON_SRC)/*.c)
KERSOURCESCPP = $(wildcard $(KER_SRC)/*.cpp)
DINOBYTESOURCES = $(wildcard $(DINOBYTE)/src/*.cpp)
#KERSOURCESCPP = $(wildcard $(KER_SRC)/$(ARCHDIR)/*.cpp)
#KERMENUSOURCESC = $(wildcard $(KER_MENU_SRC)/*.c)
#KERMENUSOURCESCPP = $(wildcard $(KER_MENU_SRC)/*.cpp)
UISOURCES = $(wildcard $(UI_IMAGES)/*.png)
OBJECTS = $(patsubst $(KER_SRC)/%.s, $(OBJ_DIR)/%.o, $(ASMSOURCES))
#OBJECTS += $(patsubst $(KER_SRC)/%.s, $(OBJ_DIR)/%.o, $(ASMSOURCES))
OBJECTS += $(patsubst $(KER_SRC)/%.c, $(OBJ_DIR)/%.o, $(KERSOURCES))
OBJECTS += $(patsubst $(KER_SRC)/%.cpp, $(OBJ_DIR)/%.o, $(KERSOURCESCPP))
OBJECTS += $(patsubst $(COMMON_SRC)/%.c, $(OBJ_DIR)/%.o, $(COMMONSOURCES))
#OBJECTS += $(patsubst $(KER_MENU_SRC)/%.c, $(OBJ_DIR)/%.o, $(KERMENUSOURCESC))
#OBJECTS += $(patsubst $(KER_MENU_SRC)/%.cpp, $(OBJ_DIR)/%.o, $(KERMENUSOURCESCPP))
#OBJECTS += $(patsubst $(UI_IMAGES)/%.png, $(OBJ_DIR)/%.o, $(UISOURCES))
#Dinobyte objects [include Dinobyte headers here] (do later)
#OBJECTS += $(patsubst $(DINOBYTESOURCES)/src/%.cpp, $(OBJ_DIR)/%.o, $(DINOBYTESOURCES))
#Headers
HEADERS = $(wildcard $(KER_HEAD)/*.h)
IMG_NAME=SuperPiOS
#build: $(OBJECTS) $(HEADERS)
#$(CC) -T linker.ld -o $(IMG_NAME).elf $(LFLAGS) $(OBJECTS) #needs indent
#$(OBJCOPY) $(IMG_NAME).elf -O binary $(IMG_NAME).img #needs indent
#$(OBJ_DIR)/%.o: $(KER_SRC)/%.s
#mkdir -p $(#D) #needs indent
#$(CC) $(CFLAGS) -I$(KER_SRC) -c $< -o $# #needs indent
$(OBJ_DIR)/%.o: $(KER_SRC)/%.s
$(GCCPATHBIN)/aarch64-none-elf-gcc $(GCCFLAGS) -c $(KER_SRC) -o $(OBJ_DIR)
$(OBJ_DIR)/%.o: $(KER_SRC)/%.c
$(GCCPATHBIN)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $#
$(OBJ_DIR)/%.o: $(KER_SRC)/$(ARCHDIR)/%.c
$(GCCPATHBIN)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $#
$(OBJ_DIR)/%.o: $(KER_SRC)/%.cpp
$(GCCPATHBIN) arm-none-eabi-cpp $(GCCFLAGS) -c $< -o $#
$(OBJ_DIR)/%.o: $(KER_SRC)/$(ARCHDIR)/%.cpp
$(GCCPATHBIN)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $#
$(OBJ_DIR)/%.o: $(COMMON_SRC)/%.c
$(GCCPATHBIN)/aarch64-none-elf-gcc $(GCCFLAGS) -c $< -o $#
#$(OBJ_DIR)/%.o: $(KER_MENU_SRC)/%.c
# mkdir -p $(#D)
# $(CC) $(CFLAGS) -I$(KER_SRC) -I$(KER_HEAD) -c $< -o $# $(CSRCFLAGS)
#$(OBJ_DIR)/%.o: $(KER_MENU_SRC)/%.cpp
# mkdir -p $(#D)
# $(CC) $(CXXFLAGS) -I$(KER_SRC) -I$(KER_HEAD) -c $< -o $# $(CSRCFLAGS)
$(IMG_NAME)%.img: $(OBJECTS) $(HEADERS)
$(GCCPATHBIN)/aarch64-none-elf-ld -nostdlib -nostartfiles $(OBJECTS) -T linker.ld -o $(IMG_NAME).elf
$(GCCPATHBIN)/aarch64-none-elf-objcopy -O binary $(IMG_NAME).elf $(IMG_NAME).img
clean:
rm -rf $(OBJ_DIR)
rm -rf $(IMG_NAME).elf
rm $(IMG_NAME).img
run: build
qemu-system-arm -m 128 -no-reboot -M raspi4 -serial stdio -kernel kernel.elf
dbg:
$(GDB) kernel.elf
dbgrun: build gdbinit
qemu-system-arm -m 128 -no-reboot -M raspi4 -serial stdio -kernel kernel.elf -S -s
.PHONY: gdbinit
gdbinit:
echo "target remote localhost:1234" > .gdbinit
echo "break kernel_main" >> .gdbinit
You have two problems. The most obvious one is that this command:
rm SuperPiOS.img
is failing because that's how the rm program is defined: if the file you ask it to delete doesn't exist then rm will fail. If you don't want that to happen, add the -f option:
rm -f SuperPiOS.img
Now if the file doesn't exist, rm will silently succeed. You can examine the man pages for the rm program.
However, the higher level question you are probably asking is, why when I run make is it running the clean rule?
That's because make, if you don't specify what to build on the command line, always builds the first explicit target. In this makefile the first explicit target you defined is clean, so that's what's built. This is almost certainly not what you want. You should figure out what target you want to build when you run make with no arguments and put the rule for that target as the first explicit target in your makefile.
Other than this rule it normally doesn't matter much what order rules come in (if you have multiple pattern rules that could all build the same target, for example multiple pattern rules to build %.o, then order can matter there).

makefile error: “make: *** No rule to make target …”

I'm trying to use GCC (linux) with a makefile to compile my project.
I get the following error
"No rule to make target 'output/src/main.o', needed by 'test'. Stop."
This is the makefile:
COMPILE_PREX ?=
CC = $(COMPILE_PREX)gcc
SOURCE = $(wildcard src/*.c)
OBJS = $(addprefix ./output/, $(patsubst %.c, %.o, $(SOURCE)))
INCLUDES = -I ./src
CFLAGS += -O2 -Wall -g
LDFLAGS += -lpthread
TARGET = test
$(TARGET): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)
%.o: %.c
#mkdir -p ./output
$(CC) $(CFLAGS) $(INCLUDES) -o $(addprefix ./output/, $#) -c $<
clean:
rm -rf ./output
This should work. Note the use of mkdir $(#D) to create output directories as needed.
COMPILE_PREX ?=
CC = $(COMPILE_PREX)gcc
SOURCE = $(wildcard src/*.c)
OBJS = $(addprefix output/, $(patsubst %.c, %.o, $(SOURCE)))
INCLUDES = -I src
CFLAGS += -O2 -Wall -g
LDFLAGS += -lpthread
TARGET = test
$(TARGET): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)
output/%.o: %.c
#mkdir -p $(#D)
$(CC) $(CFLAGS) $(INCLUDES) -o $# -c $<
clean:
rm -rf output
Running it here with make clean test:
rm -rf output
gcc -O2 -Wall -g -I src -o output/src/hello.o -c src/hello.c
gcc output/src/hello.o -lpthread -o test

Issue with Makefile recipe for inputting object files

I have a makefile, that I edited so that object files should go to a separate directory.
I edited the makefile and it is now doing what I wanted to, but during linker stage it is not working as expected because I am doing something wrong.
I have a sources file and makefile as shown below:
Sources File
TARGET = demo
SRC_DIR = modbus/ascii/ \
modbus/functions/ \
modbus/port/ \
modbus/rtu/ \
modbus/ \
./
INCLUDE_DIR = modbus/include/ \
modbus/port/ \
modbus/rtu \
modbus/ascii \
./
SOURCE = modbus/ascii/mbascii.c \
modbus/functions/mbfunccoils.c \
modbus/functions/mbfuncdiag.c \
modbus/functions/mbfuncdisc.c \
modbus/functions/mbfuncholding.c \
modbus/functions/mbfuncinput.c \
modbus/functions/mbfuncother.c \
modbus/functions/mbutils.c \
modbus/port/port.c \
modbus/port/portevent.c \
modbus/port/portserial.c \
modbus/port/porttimer.c \
modbus/rtu/mbcrc.c \
modbus/rtu/mbrtu.c \
modbus/mb.c \
demo.c \
startup_LPC17xx.c \
system_LPC17xx.c
Makefile
include ./sources
ARCH = arm-none-eabi
BUILD_DIR := build/objs
# Tool definitions
CC = $(ARCH)-gcc
LD = $(ARCH)-gcc
AR = $(ARCH)-ar
AS = $(ARCH)-as
CP = $(ARCH)-objcopy
OD = $(ARCH)-objdump
SIZE = $(ARCH)-size
RM = rm
Q = # #./quiet "$#"
# Flags
CFLAGS = -W -Wall -O0 --std=gnu99 -fgnu89-inline -mcpu=cortex-m3 -mthumb
CFLAGS += -ffunction-sections -fdata-sections
ASFLAGS =
LDFLAGS = -nostartfiles -specs=nosys.specs
CPFLAGS =
ODFLAGS = -x --syms
PRFLAGS ?=
# Source files
LINKER_SCRIPT = LPC17xx.ld
OBJS = $(SOURCE:.c=.o)
BUILD_OBJS := $(patsubst %,$(BUILD_DIR)/%,$(SOURCE:.c=.o))
OBJ_DIR := $(dir $(BUILD_OBJS))
INC_PARAMS = $(foreach d, $(INCLUDE_DIR), -I$d)
print-% : ; #echo $* = $($*)
.PHONY: all size clean nuke
all: $(TARGET).bin $(TARGET).hex
isp: $(TARGET).bin
# #./quiet $< cp $^ $(MBED_VOLUME)/
size: $(TARGET).elf
#$(SIZE) $<
%.hex: %.elf
$Q $(CP) $(CPFLAGS) -O ihex $< $*.hex
%.bin: %.elf
$Q $(CP) $(CPFLAGS) -O binary $< $*.bin
$(TARGET).elf: $(OBJS)
#touch $(#:.elf=.map)
$Q $(LD) -Xlinker -Map $(#:.elf=.map) $(LDFLAGS) -T $(LINKER_SCRIPT) $(BUILD_DIR)/$^ -o $#
$Q $(OD) $(ODFLAGS) $# > $(#:.elf=.dump)
#$(SIZE) $#
$(OBJS): %.o: %.c
mkdir -p $(dir $(BUILD_OBJS))
$Q $(CC) $(CFLAGS) $(INC_PARAMS) -c $< -o $(BUILD_DIR)/$#
.PHONY: clean
clean: CRUFT=$(shell find . -name '*.o' -o -name '*.d')
clean: ; rm -f $(CRUFT); rm -f *.elf *.hex *.bin *.dump *.map; rm -rf $(BUILD_DIR)/*
nuke: clean
-rm -f *.hex *.bin *.dump *.map
The issue is in line:
$Q $(LD) -Xlinker -Map $(#:.elf=.map) $(LDFLAGS) -T $(LINKER_SCRIPT) $(BUILD_DIR)/$^ -o $#
For which the output is:
arm-none-eabi-gcc -Xlinker -Map demo.map -nostartfiles -specs=nosys.specs -T LPC17xx.ld build/objs/modbus/ascii/mbascii.o modbus/functions/mbfunccoils.o modbus/functions/mbfuncdiag.o modbus/functions/mbfuncdisc.o modbus/functions/mbfuncholding.o modbus/functions/mbfuncinput.o modbus/functions/mbfuncother.o modbus/functions/mbutils.o modbus/port/port.o modbus/port/portevent.o modbus/port/portserial.o modbus/port/porttimer.o modbus/rtu/mbcrc.o modbus/rtu/mbrtu.o modbus/mb.o demo.o startup_LPC17xx.o system_LPC17xx.o -o demo.elf
The output command picks only the first object file from correct directory under build/objs.
Rest object file path doesn't have build/objs/.
Please anyone help me resolve this issue?
You are violating Mad Scientist's second rule of makefiles, and inviting more problems than you know.
Look at these rules (simplified):
$(TARGET).elf: $(OBJS)
$(LD) $(BUILD_DIR)/$^ -o $#
$(OBJS): %.o: %.c
$(CC) -c $< -o $(BUILD_DIR)/$#
Suppose the build directory is build/ and the object file is build/foo.o. The target of the second rule is foo.o, but what it actually builds is build/foo.o. Likewise, the first rule claims foo.o as a prerequisite, but it doesn't actually use foo.o, it uses build/foo.o These two errors cancel each other out, in a sense; Make succeeds in building the main target. But as you have found, it has trouble if there is more than one object file, because if the prerequisite list is foo.o bar.o, then
`$(BUILD_DIR)/$^`
expands to
build/foo.o bar.o
Also, Make will run the second rule even if build/foo.o exists and is up to date, and will fail to run it if foo.o exists. The target of a non-PHONY rule should be the name of the file it builds, and a non-PHONY prerequisite should be the name of a file whose existence is relevant:
$(TARGET).elf: $(BUILD_OBJS)
$(LD) $^ -o $#
$(BUILD_OBJS): $(BUILD_DIR)/%.o: %.c
$(CC) -c $< -o $#

Makefile: How to place auto-generated files in a separate directory?

You can see my Makefile here:
....
PHY_SRCS = ../LwMesh/phy/at86rf212/src/phy.c
HAL_SRCS = ../LwMesh/hal/atmega1281/src/hal.c ../LwMesh/hal/atmega1281/src/halPhy.c ../LwMesh/hal/atmega1281/src/halTimer.c
SYS_SRCS = ../LwMesh/sys/src/sys.c ../LwMesh/sys/src/sysEncrypt.c ../LwMesh/sys/src/sysTimer.c
....
# define the C object files
PHY_OBJS = $(PHY_SRCS:.c=.o)
HAL_OBJS = $(HAL_SRCS:.c=.o)
....
# define the executable file
PHY = phy_cc
HAL = hal_cc
SYS = sys_cc
.....
OBJDIR := obj
$(OBJDIR):
mkdir $(OBJDIR)
all: $(PHY) $(HAL) $(SYS) $(DRV) $(NWK) $(SRV) $(VELA)
#echo Alles wurde Kompiliert
$(PHY): $(PHY_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(PHY) $(PHY_OBJS) $(LFLAGS)
$(HAL): $(HAL_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(HAL) $(HAL_OBJS) $(LFLAGS)
....
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $#
...
I have tried too much to place auto-generated files (e.g. object files) in a separate directory(OBJDIR), but I did not find any solution.
I referred to this tutorial, but it is not suitable for my Makefile.
Could you please help me?
Let's take this in small steps.
You already have a rule for building object files:
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $#
Which can be written out as:
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $#
This will use ../LwMesh/phy/at86rf212/src/phy.c to build ../LwMesh/phy/at86rf212/src/phy.c. But imagine for a moment that phy.c were in the working directory. Then Make could use that rule to build phy.o from phy.c. In that case we could make a small change in the rule:
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $#
and Make would build obj/phy.o.
How do we tell Make where to find phy.c? There are two good ways. One is using vpath:
vpath %.c ../LwMesh/phy/at86rf212/src
After we confirm that this works, we can add other directories for other sets of objects (such as PHY and SYS):
vpath %.c ../LwMesh/phy/at86rf212/src ../LwMesh/hal/atmega1281/src ../LwMesh/sys/src ...
Now all we have to do is construct the lists of objects correctly:
PHY_OBJS = phy.o
HAL_OBJS = hal.o halPhy.o halTimer.o
SYS_OBJS = sys.o sysEncrypt.o sysTimer.o
...
Once this much is working correctly, further refinements are possible.
You can see the corrected Makefile here:
# define compiler type
CC = avr-gcc
# define any compile-time flags
CFLAGS = -Wall -g -funsigned-char -funsigned-bitfields -DPHY_AT86RF212 -DHAL_ATMEGA1281 -DPLATFORM_ANY900_STICK -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -mrelax -g2 -Wall -mmcu=atmega1281 -c -std=gnu99 -MD -MP -MF "$(#:%.o=%.d)" -MT"$(#:%.o=%.d)" -MT"$(#:%.o=%.o)"
# define any directories containing header files other than /usr/include
INCLUDES += \
-I"../LwMesh/hal/atmega1281/inc" \
-I"../LwMesh/hal/drivers/atmega1281" \
-I"../LwMesh/phy/at86rf212/inc" \
-I"../LwMesh/nwk/inc" \
-I"../LwMesh/sys/inc" \
-I"../LwMesh/service/inc" \
-I"../common" \
-I".." \
-I.
# define library paths in addition to /usr/lib
LFLAGS = -L/home/newhall/lib -L../lib
# define any libraries to link into executable:
LIBS = -lmylib -lm
# define the C source files
PHY_SRCS = ../LwMesh/phy/at86rf212/src/phy.c
HAL_SRCS = ../LwMesh/hal/atmega1281/src/hal.c ../LwMesh/hal/atmega1281/src/halPhy.c ../LwMesh/hal/atmega1281/src/halTimer.c
SYS_SRCS = ../LwMesh/sys/src/sys.c ../LwMesh/sys/src/sysEncrypt.c ../LwMesh/sys/src/sysTimer.c
DRV_SRCS = ../LwMesh/hal/drivers/atmega1281/halUart.c ../LwMesh/hal/drivers/atmega1281/halTwi.c
NWK_SRCS += \
../LwMesh/nwk/src/nwk.c \
../LwMesh/nwk/src/nwkDataReq.c \
../LwMesh/nwk/src/nwkSecurity.c \
../LwMesh/nwk/src/nwkFrame.c \
../LwMesh/nwk/src/nwkGroup.c \
../LwMesh/nwk/src/nwkRoute.c \
../LwMesh/nwk/src/nwkRouteDiscovery.c \
../LwMesh/nwk/src/nwkRx.c \
../LwMesh/nwk/src/nwkTx.c
SRV_SRCS = ../LwMesh/service/src/otaClient.c ../LwMesh/service/src/otaServer.c
VELA_SRCS += \
base-commands.c \
bus-commands.c \
bus-interface.c \
host-interface.c \
measurement.c \
VelaMain.c \
otaInterface.c \
persistence.c \
shell.c
PHY_OBJS = $(addprefix $(OBJDIR)/PHY/,phy.o)
HAL_OBJS = $(addprefix $(OBJDIR)/HAL/,hal.o halPhy.o halTimer.o)
SYS_OBJS = $(addprefix $(OBJDIR)/SYS/,sys.o sysEncrypt.o sysTimer.o)
DRV_OBJS = $(addprefix $(OBJDIR)/DRV/,halUart.o halTwi.o)
NWK_OBJS = $(addprefix $(OBJDIR)/NWK/,nwk.o nwkDataReq.o nwkSecurity.o nwkFrame.o nwkGroup.o nwkRoute.o nwkRouteDiscovery.o nwkRx.o nwkTx.o)
SRV_OBJS = $(addprefix $(OBJDIR)/SRV/,otaClient.o otaServer.o)
VELA_OBJS = $(addprefix $(OBJDIR)/VELA/,base-commands.o bus-commands.o bus-interface.o host-interface.o measurement.o VelaMain.o otaInterface.o persistence.o shell.o)
# define the executable file
PHY = phy_cc
HAL = hal_cc
SYS = sys_cc
DRV = drv_cc
NWK = nwk_cc
SRV = srv_cc
VELA = vela_cc
OBJDIR := objdir
OUTPUT_FILE_PATH +=Vela2.elf
OUTPUT_FILE_PATH_AS_ARGS +=Vela2.elf
OUTPUT_FILE_DEP:=
ALL_OBJS := $(PHY_OBJS) $(HAL_OBJS) $(SYS_OBJS) $(DRV_OBJS) $(NWK_OBJS) $(SRV_OBJS) $(VELA_OBJS)
ALL_SRCS := $(PHY_SRCS) $(HAL_SRCS) $(SYS_SRCS) $(DRV_SRCS) $(NWK_SRCS) $(SRV_SRCS) $(VELA_SRCS)
$(OUTPUT_FILE_PATH): $(ALL_OBJS) $(OUTPUT_FILE_DEP)
#echo Invoking: AVR/GNU Linker
$(CC) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(ALL_OBJS) -Wl,-Map="Vela2.map" -Wl,-u,vfprintf -Wl,--start-group -Wl,--end-group -Wl,--gc-sections -mrelax -mmcu=atmega1281
#echo Finished building target: $#
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Vela2.elf" "Vela2.hex"
avr-objcopy -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "Vela2.elf" "Vela2.eep" || exit 0
avr-objdump -h -S "Vela2.elf" > "Vela2.lss"
avr-objcopy -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Vela2.elf" "Vela2.srec"
avr-size "Vela2.elf"
$(OBJDIR)/PHY/%.o : ../LwMesh/phy/at86rf212/src/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
$(OBJDIR)/HAL/%.o : ../LwMesh/hal/atmega1281/src/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
$(OBJDIR)/SYS/%.o : ../LwMesh/sys/src/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
$(OBJDIR)/DRV/%.o : ../LwMesh/hal/drivers/atmega1281/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
$(OBJDIR)/NWK/%.o : ../LwMesh/nwk/src/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
$(OBJDIR)/SRV/%.o : ../LwMesh/service/src/%.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
$(OBJDIR)/VELA/%.o : %.c
$(CC) $(CFLAGS) $(INCLUDES) $(OUTPUT_OPTION) $<
all: $(ALL_OBJS) $(OUTPUT_FILE_PATH)
$(ALL_OBJS): | $(OBJDIR)
$(OBJDIR):
mkdir -p objdir/PHY
mkdir -p objdir/HAL
mkdir -p objdir/SYS
mkdir -p objdir/DRV
mkdir -p objdir/NWK
mkdir -p objdir/SRV
mkdir -p objdir/VELA
$(PHY): $(PHY_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(PHY) $(PHY_OBJS) $(LFLAGS)
$(HAL): $(HAL_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(HAL) $(HAL_OBJS) $(LFLAGS)
$(SYS): $(SYS_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(SYS) $(SYS_OBJS) $(LFLAGS)
$(DRV): $(DRV_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(DRV) $(DRV_OBJS) $(LFLAGS)
$(NWK): $(NWK_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(NWK) $(NWK_OBJS) $(LFLAGS)
$(SRV): $(SRV_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(SRV) $(SRV_OBJS) $(LFLAGS)
$(VELA): $(VELA_OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(VELA) $(VELA_OBJS) $(LFLAGS)
clean:
-$(RM) $(OBJS_AS_ARGS) $(EXECUTABLES)
-$(RM) $(C_DEPS_AS_ARGS)
rm -rf "Vela2.elf" "Vela2.a" "Vela2.hex" "Vela2.lss" "Vela2.eep" "Vela2.map" "Vela2.srec" "Vela2.usersignatures"
depend: $(PHY_SRCS) $(HAL_SRCS) $(SYS_SRCS) $(DRV_SRCS) $(NWK_SRCS) $(SRV_SRCS) $(VELA_SRCS)
makedepend $(INCLUDES) $^

Makefile recompiles everything

I know this question have been up multiple times but I can't seem to find my problem anyway.
The object and dependency files are created in their own folders. But it seems that they are not used when recompiling. What have I done wrong?
I have followed http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#combine for the generation of dependency files.
APP = test
OBJDIR = obj
DEPDIR = dep
HDRS = -Ibuild/src_generated -Isrc -Isrc/client -Isrc/server -Ideps -Iinclude -Iplugins
SRCS = $(wildcard build/src_generated/*.c) \
$(wildcard src/*.c) \
$(wildcard src/client/*.c) \
$(wildcard src/server/*.c) \
$(wildcard deps/*.c) \
plugins/ua_accesscontrol_default.c \
plugins/ua_clock.c \
plugins/ua_config_default.c \
plugins/ua_debug_dump_pkgs.c \
plugins/ua_log_stdout.c \
plugins/ua_network_tcp.c \
plugins/ua_nodestore_default.c \
plugins/ua_pki_certificate.c \
plugins/ua_securitypolicy_none.c \
tutorial_server_datasource.c
SRCDIRS = ./build/src_generated ./src ./src/client ./src/server ./deps ./plugins
OBJS = $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
POSTCOMPILE = #mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $#
CFLAGS = -Os -std=c99 -DUA_NO_AMALGAMATION
DEPFLAGS = -MT $# -MMD -MP -MF $(DEPDIR)/$*.Td
$(APP): buildrepo $(OBJS)
$(CC) $(OBJS) -o $# $(CFLAGS) $(HDRS) -s
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
$(CC) $(DEPFLAGS) $(CFLAGS) $(HDRS) -o $# -c $<
$(POSTCOMPILE)
$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d
include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
.PHONY: clean
clean:
rm -rf $(OBJDIR)/*
rm -rf $(DEPDIR)/*
rm -rf $(APP)
buildrepo:
#$(call make-repo)
define make-repo
for dir in $(SRCDIRS); \
do \
mkdir -p $(OBJDIR)/$$dir; \
mkdir -p $(DEPDIR)/$$dir; \
done
endef
This line:
include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
Expects that all dependency files are in one folder. You probably want:
-include ${SRCS:%.c=$(OBJDIR)/%.d} # Note the leading -

Resources