在Makefile中缺少src代码文件

时间:2012-09-21 07:48:54

标签: c makefile

我的Makefile有一个非常令人困惑的问题

我正在使用它为linux和windows

构建一个带有条件编译的单元测试可执行文件

对于从src代码构建目标文件的规则,它缺少某些文件。似乎没有一个模式,例如所有丢失的文件来自特定目录 - 它似乎随机发出一些我的src文件。

我的Makefile看起来像这样:

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)

当我进行干运行(make -n)时,我得到以下输出:

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

请注意,minIni.c, ddi.c, mem.c, pulseIO.c, lib.c and bsd_offset.c未编译到其repsective对象文件

我已经检查过src代码文件实际存在于相关目录中,因此我必须在Makefile中做错了但是我无法发现它是什么......

更新

更改了这一行:

$(BUILD_DIR)/%.o: %.c

有:

$(program_OBJS): $(program_C_SRCS)

这会引入所有* .o文件但在每个实例中使用main.c,即输出看起来像这样:

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

1 个答案:

答案 0 :(得分:1)

如果输入文件比输出文件旧,则make将不会构建它。 因此make不会在构建之间编译未经编译的C文件。这加快了编译速度。

尝试以下方法:

make clean all