0

我在 Makefile 中添加了另一条规则,以尝试构建一个 C 共享库,该库使用 SWIG 使用 JNI 为 Java 包装函数

附加规则如下所示(基本上来自 SWIG java 示例之一)

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

我遇到的问题是我的 VPATH 似乎不再适用于 *.c 文件

我注意到此规则导致所有 .c 文件编译为对 gcc 的一次调用,而不是对 gcc 的单独调用以编译每个 .o 文件

我以前没有任何 JNI 东西的编译规则如下所示:

.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 $@

这些与VPATH一起工作没问题

我的 VPATH 语句如下所示:

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

1 回答 1

2

看看你的规则:

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.)

于 2012-07-31T14:13:36.257 回答