将cpp make文件修改为.c文件无效

时间:2015-04-18 02:13:39

标签: linux makefile subst

我正在尝试修改将cpp构建到c文件的makefile,但是我遇到了一些字符串替换问题。我想知道是否有人可以指出错误。

这是文件的一部分:

SOURCES := \
lz4.c \
lz4frame.c \
lz4hc.cpp \
xxhash.c

OBJECTS := $(addprefix $(OBJ_DIR)/,$(subst .c,.o,$(SOURCES)))
DEPENDS := $(addprefix $(OBJ_DIR)/,$(subst .c,.d,$(SOURCES)))

all: $(OUT_DIR)/$(LIB_NAME)

clean:

rm -rf $(OBJ_DIR)
rm -rf $(OUT_DIR)

$(OUT_DIR)/$(LIB_NAME): $(OBJECTS)
    @rm -f $@
    $(AR) cr $@ $^

$(OBJECTS): $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
    @mkdir -p $(@D)
    $(CXX) -MMD -MF $(OBJ_DIR)/$*.d -MP -MT'$(OBJ_DIR)/$*.o $(OBJ_DIR)/$*.d' -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

.PHONY: all clean

-include $(DEPENDS)

但是当我输入make clean时我得到了这个:

 Makefile:38: target `Release64/obj/lz4hc.opp' doesn't match the target pattern

或者当我尝试构建时,我得到了这个:

g++-4.6 -MMD -MF ./Release64/obj/Release64/obj/lz4hc.opp.d -MP -MT'./Release64/obj/Release64/obj/lz4hc.opp.o ./Release64/obj/Release64/obj/lz4hc.opp.d' -c -I../../../include -I../../../thirdparty/include/lz4 -std=c++0x -fPIC -O2 -m64  -o Release64/obj/lz4hc.opp
g++-4.6: fatal error: no input files

编译终止。

我认为问题在于这条线,但我无法弄清楚问题:

OBJECTS := $(addprefix $(OBJ_DIR)/,$(subst .c,.o,$(SOURCES)))
DEPENDS := $(addprefix $(OBJ_DIR)/,$(subst .c,.d,$(SOURCES)))

THX!

3 个答案:

答案 0 :(得分:2)

您的sources变量是:

SOURCES := \
lz4.c \
lz4frame.c \
lz4hc.cpp \
xxhash.c

请注意文件名lz4hc.cpp。然后,您将.c替换为.o,这会为您提供lz4hc.opp的文件名,这正是您看到的错误。

目前尚不清楚您是否真的想要混合使用C和C ++文件,或者您只是忘记更改变量中某个文件的名称。

答案 1 :(得分:0)

您可以将C和C ++源分成单独的列表,并使用模式规则来构建源,例如:

SOURCES_C := \
lz4.c \
lz4frame.c \
xxhash.c
SOURCES_CPP:= lz4hc.cpp
SOURCES:=$(SOURCES_C) $(SOURCES_CPP)

OBJECTS_C := $(addprefix $(OBJ_DIR)/,$(subst .c,.o,$(SOURCES_C)))
OBJECTS_CPP:=$(addprefix $(OBJ_DIR)/,$(subst .cpp,.o,$(SOURCES_CPP)))
OBJECTS:=$(OBJECTS_C) $(OBJECTS_CPP)

DEPENDS_C := $(addprefix $(OBJ_DIR)/,$(subst .c,.d,$(SOURCES_C)))
DEPENDS_CPP := $(addprefix $(OBJ_DIR)/,$(subst .cpp,.d,$(SOURCES_CPP)))
DEPENTS:=$(DEPENDS_C) $(DEPENDS_CPP)

all: $(OUT_DIR)/$(LIB_NAME)

clean:
    $(RM) $(OBJECTS) $(DEPENDS)

$(OUT_DIR)/$(LIB_NAME): $(OBJECTS)
    @rm -f $@
    $(AR) cr $@ $^

%.o: %.c
    @mkdir -p $(@D)
    $(CC) -MMD -MF $(OBJ_DIR)/$*.d -MP -MT'$(OBJ_DIR)/$*.o $(OBJ_DIR)/$*.d' -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

%.o: %.cpp
    @mkdir -p $(@D)
    $(CXX) -MMD -MF $(OBJ_DIR)/$*.d -MP -MT'$(OBJ_DIR)/$*.o $(OBJ_DIR)/$*.d' -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

.PHONY: all clean

-include $(DEPENDS)

答案 2 :(得分:0)

您的文件以.cpp结尾,但您的subst命令仅处理以.c结尾的名称。尝试:

OBJECTS := $(addprefix $(OBJ_DIR)/,$(subst .c,.o,$(subst .cpp,.o,$(SOURCES))))
DEPENDS := $(addprefix $(OBJ_DIR)/,S(subst .c,.d,$(subst .cpp,.d,$(SOURCES))))