makefilevpath

VPATH not working with Makefile Rule


I have added another rule to a Makefile to attempt to build a C shared library that uses SWIG to wrap the functions for Java using JNI

The additional rule looks like this (basically lifted from one of the SWIG java examples)

java: $(program_C_SRCS)
    $(SWIG) -java $(SWIGOPT) $(INTERFACEPATH)
    $(CC) -c $(CFLAGS) $(JAVACFLAGS) $(program_C_SRCS) $(ISRCS) $(CPPFLAGS) $(JAVA_INCLUDE)
    $(JAVALDSHARED) $(CFLAGS) $(program_C_OBJS) $(IOBJS) $(JAVA_DLNK) $(LDFLAGS) -o $(JAVA_LIBPREFIX)$(TARGET)$(JAVASO)
    javac *.java

problem I have is that my VPATH doesn't seem to work with the *.c files anymore

I noticed that this rule causes all the .c files to compiled as one call to gcc rather than a separate call to gcc for the compilation of each .o file

my previous rules for compilation without any JNI stuff look like this:

.PHONY: all clean

all: $(program_DEBUG_NAME) $(program_RELEASE_NAME)

# original rule to build library in src dir (no longer inc. in all)
$(program_NAME): $(program_C_OBJS)
    $(LINK.c) -shared -Wl,-soname,$@ $^ -o $@

# new rules to build debug/release libraries and place them in relevant build
# dirs
$(program_DEBUG_NAME): $(DEBUG_OBJS)
    $(DEBUG_LINK.c) -shared -Wl,-soname,$@ $^ -o $(BUILD_DIR)/debug/$@

$(program_RELEASE_NAME): $(RELEASE_OBJS)
    $(RELEASE_LINK.c) -shared -Wl,-soname,$@ $^ -o $(BUILD_DIR)/release/$@

# rule to build object files (replaces implicit rule)
$(BUILD_DIR)/debug/%.o: %.c
    $(DEBUG_LINK.c) $< -c -o $@

$(BUILD_DIR)/release/%.o: %.c
    $(RELEASE_LINK.c) $< -c -o $@

and these work with VPATH no problem

my VPATH statement looks like this:

VPATH = ../../pulse_IO/src ../../../g2/src

Solution

  • Look at your rule:

    java: $(program_C_SRCS)
        ...
        $(CC) -c $(CFLAGS) $(JAVACFLAGS) $(program_C_SRCS) ...
        ...
    

    Suppose program_C_SRCS is foo.c, and the path is somewhere/foo.c. Without VPATH, this rule doesn't run at all because Make can't find foo.c. With VPATH, Make finds it, and knows that the real prereq is somewhere/foo.c. But you have $(program_C_SRCS) in your rule:

    java: somewhere/foo.c
        ...
        $(CC) -c $(CFLAGS) $(JAVACFLAGS) foo.c ...
        ...
    

    This fails because there is no foo.c (locally).

    Try this:

    java: $(program_C_SRCS)
        ...
        $(CC) -c $(CFLAGS) $(JAVACFLAGS) $^ ...
        ...
    

    (The use of automatic variables like $^ is the reason your previous rules worked.)