I have a really confusing issue with my Makefile
I am using it to build a unit-test executable with conditional compilation for linux and windows
For the rule where it is building the object files from the src code it is missing out certain files. There doesn’t seem to be a pattern to it such as all the missing files are from a specific directory – it just appears to randomly iss out some of my src files.
My Makefile looks like this:
TARGET := test_glamdring2
program_NAME := $(TARGET)
#WIN32 - need to install hg on WIN32 platform for this line to work
#HGVERSION:= $(shell hg parents --template 'hgid: {node|short}')
# Platform specific conditional compilation
UNAME := $(shell uname)
# specify dirs other then current dir to search for src files
VPATH = ../src ../../RESTRICTED/core/src ../../RESTRICTED/pulse_IO/src
# src code file locations realtive to test/ dir
# SRCS = main.c \
# test_utility.c \
# ../src/utility.c \
# test_tha.c \
# ../src/tha.c \
# ../src/minIni.c \
# test_load_config.c \
# ../src/load_config.c \
# test_ddi.c \
# ../src/ddi.c \
# ../src/caa.c \
# ../../RESTRICTED/core/src/mem.c \
# ../../RESTRICTED/pulse_IO/src/pulse_IO.c \
# test_caa.c \
# test_save_library.c \
# ../src/save_library.c \
# ../../RESTRICTED/core/src/load_gnf.c \
# ../../RESTRICTED/core/src/loadLibrary.c \
# test_lib.c \
# ../src/lib.c \
# ../../RESTRICTED/core/src/init_tdd.c \
# ../../RESTRICTED/core/src/tdd.c \
# ../../RESTRICTED/core/src/bsd_offset.c \
# test_utarray.c \
# test_load_glf.c \
# ../src/load_glf.c
# flattened src code file locations for use with VPATH
SRCS = main.c \
test_utility.c \
utility.c \
test_tha.c \
tha.c \
minIni.c \
test_load_config.c \
load_config.c \
test_ddi.c \
ddi.c \
caa.c \
mem.c \
pulse_IO.c \
test_caa.c \
test_save_library.c \
save_library.c \
load_gnf.c \
loadLibrary.c \
test_lib.c \
lib.c \
init_tdd.c \
tdd.c \
bsd_offset.c \
test_utarray.c \
test_load_glf.c \
load_glf.c
program_C_SRCS := $(SRCS)
program_C_OBJS := ${program_C_SRCS:.c=.o}
#program_OBJS := $(program_C_OBJS)
ifeq ($(UNAME), Linux)
# LINUX version
program_INCLUDE_DIRS := \
/home/ben/projects/glamdring/RESTRICTED/core/src \
/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src \
/home/ben/projects/glamdring/g2/src
BUILD_DIR = ../build/linux
endif
ifeq ($(UNAME), MINGW32_NT-6.1)
# WINDOWS ESROE-5 VirtualBox version
program_INCLUDE_DIRS := \
E:/projects/glamdring/g2/src \
E:/projects/glamdring/RESTRICTED/pulse_IO/src \
E:/projects/glamdring/RESTRICTED/core/src
program_LIBRARY_DIRS :=C:/CUnit-2.1-2/lib
BUILD_DIR = ../build/windows
endif
# ensure object files are preceded by the correct build dir
program_OBJS := $(addprefix $(BUILD_DIR)/,${program_C_OBJS})
program_LIBRARIES := cunit m
CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))
# Non debug version
#CFLAGS += -O2 -Wallx
# Debug version (with -g)
# NB do not use optimisation (-O2) for debugging with gdb
#CFLAGS += -g -Wall -DHGVERSION="\"${HGVERSION}\"" -DDEBUG=0
CFLAGS += -g -Wall -DDEBUG=0
# NB $(LDFLAGS) moved from LINK.c to after $(programOBJS) below
# does not work if -lcunit appears before *.o files
LINK.c := $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH)
.PHONY: all clean distclean
all: $(program_NAME)
# $(program_NAME): $(program_OBJS)
# $(LINK.c) $(program_OBJS) $(LDFLAGS) -o $(program_NAME)
$(program_NAME): $(program_OBJS)
$(LINK.c) $(program_OBJS) $(LDFLAGS) -o $(BUILD_DIR)/$@
# rule to build object files (replaces implicit rule)
# ensures object files go to the platform conditional dir
# ../build/linux ro ../build/linux
$(BUILD_DIR)/%.o: %.c
echo $@
$(LINK.c) $< -c -o $@
clean:
@- $(RM) $(program_NAME)
@- $(RM) $(program_OBJS)
distclean: clean
valgrind: $(program_NAME)
valgrind --tool=memcheck --leak-check=yes ./$(program_NAME)
when I do a dry-run (make -n) I get the following output:
echo ../build/linux/main.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/main.o
echo ../build/linux/test_utility.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_utility.c -c -o ../build/linux/test_utility.o
echo ../build/linux/utility.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../src/utility.c -c -o ../build/linux/utility.o
echo ../build/linux/test_tha.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_tha.c -c -o ../build/linux/test_tha.o
echo ../build/linux/tha.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../src/tha.c -c -o ../build/linux/tha.o
echo ../build/linux/test_load_config.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_load_config.c -c -o ../build/linux/test_load_config.o
echo ../build/linux/load_config.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../src/load_config.c -c -o ../build/linux/load_config.o
echo ../build/linux/test_ddi.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_ddi.c -c -o ../build/linux/test_ddi.o
echo ../build/linux/caa.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../src/caa.c -c -o ../build/linux/caa.o
echo ../build/linux/test_caa.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_caa.c -c -o ../build/linux/test_caa.o
echo ../build/linux/test_save_library.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_save_library.c -c -o ../build/linux/test_save_library.o
echo ../build/linux/save_library.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../src/save_library.c -c -o ../build/linux/save_library.o
echo ../build/linux/load_gnf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../../RESTRICTED/core/src/load_gnf.c -c -o ../build/linux/load_gnf.o
echo ../build/linux/loadLibrary.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../../RESTRICTED/core/src/loadLibrary.c -c -o ../build/linux/loadLibrary.o
echo ../build/linux/test_lib.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_lib.c -c -o ../build/linux/test_lib.o
echo ../build/linux/init_tdd.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../../RESTRICTED/core/src/init_tdd.c -c -o ../build/linux/init_tdd.o
echo ../build/linux/tdd.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../../RESTRICTED/core/src/tdd.c -c -o ../build/linux/tdd.o
echo ../build/linux/test_utarray.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_utarray.c -c -o ../build/linux/test_utarray.o
echo ../build/linux/test_load_glf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src test_load_glf.c -c -o ../build/linux/test_load_glf.o
echo ../build/linux/load_glf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../src/load_glf.c -c -o ../build/linux/load_glf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../build/linux/main.o ../build/linux/test_utility.o ../build/linux/utility.o ../build/linux/test_tha.o ../build/linux/tha.o ../build/linux/minIni.o ../build/linux/test_load_config.o ../build/linux/load_config.o ../build/linux/test_ddi.o ../build/linux/ddi.o ../build/linux/caa.o ../build/linux/mem.o ../build/linux/pulse_IO.o ../build/linux/test_caa.o ../build/linux/test_save_library.o ../build/linux/save_library.o ../build/linux/load_gnf.o ../build/linux/loadLibrary.o ../build/linux/test_lib.o ../build/linux/lib.o ../build/linux/init_tdd.o ../build/linux/tdd.o ../build/linux/bsd_offset.o ../build/linux/test_utarray.o ../build/linux/test_load_glf.o ../build/linux/load_glf.o -lcunit -lm -o ../build/linux/test_glamdring2
Note that minIni.c, ddi.c, mem.c, pulseIO.c, lib.c and bsd_offset.c are not compiled into their repsective object files
I have triple checked that the src code files are actually present in the relevant directories so I must be doing something wrong in the Makefile here but I just can’t spot what it is…
UPDATE
have changed the line:
$(BUILD_DIR)/%.o: %.c
for:
$(program_OBJS): $(program_C_SRCS)
this brings in all the *.o files but uses main.c in every instance, i.e. output looks liek this:
echo ../build/linux/main.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/main.o
echo ../build/linux/test_utility.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_utility.o
echo ../build/linux/utility.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/utility.o
echo ../build/linux/test_tha.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_tha.o
echo ../build/linux/tha.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/tha.o
echo ../build/linux/minIni.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/minIni.o
echo ../build/linux/test_load_config.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_load_config.o
echo ../build/linux/load_config.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/load_config.o
echo ../build/linux/test_ddi.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_ddi.o
echo ../build/linux/ddi.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/ddi.o
echo ../build/linux/caa.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/caa.o
echo ../build/linux/mem.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/mem.o
echo ../build/linux/pulse_IO.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/pulse_IO.o
echo ../build/linux/test_caa.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_caa.o
echo ../build/linux/test_save_library.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_save_library.o
echo ../build/linux/save_library.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/save_library.o
echo ../build/linux/load_gnf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/load_gnf.o
echo ../build/linux/loadLibrary.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/loadLibrary.o
echo ../build/linux/test_lib.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_lib.o
echo ../build/linux/lib.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/lib.o
echo ../build/linux/init_tdd.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/init_tdd.o
echo ../build/linux/tdd.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/tdd.o
echo ../build/linux/bsd_offset.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/bsd_offset.o
echo ../build/linux/test_utarray.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_utarray.o
echo ../build/linux/test_load_glf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/test_load_glf.o
echo ../build/linux/load_glf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src main.c -c -o ../build/linux/load_glf.o
cc -g -Wall -DDEBUG=0 -I/home/ben/projects/glamdring/RESTRICTED/core/src -I/home/ben/projects/glamdring/RESTRICTED/pulse_IO/src -I/home/ben/projects/glamdring/g2/src ../build/linux/main.o ../build/linux/test_utility.o ../build/linux/utility.o ../build/linux/test_tha.o ../build/linux/tha.o ../build/linux/minIni.o ../build/linux/test_load_config.o ../build/linux/load_config.o ../build/linux/test_ddi.o ../build/linux/ddi.o ../build/linux/caa.o ../build/linux/mem.o ../build/linux/pulse_IO.o ../build/linux/test_caa.o ../build/linux/test_save_library.o ../build/linux/save_library.o ../build/linux/load_gnf.o ../build/linux/loadLibrary.o ../build/linux/test_lib.o ../build/linux/lib.o ../build/linux/init_tdd.o ../build/linux/tdd.o ../build/linux/bsd_offset.o ../build/linux/test_utarray.o ../build/linux/test_load_glf.o ../build/linux/load_glf.o -lcunit -lm -o ../build/linux/test_glamdring2
If a input file is older than the output file, then make won’t build it.
So make will not compile unchaged C files between builds. That speeds up the compilation.
Try the following: