1

我的 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没有编译到它们各自的目标文件中

我已经三重检查了 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
4

1 回答 1

1

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

尝试以下操作:

make clean all
于 2012-09-21T08:03:59.197 回答