gmmo
gmmo

Reputation: 2811

modifying a cpp make file to .c file is not working

I am trying to modify a makefile that builds cpp to a c file, but I am running into some string substitution problem. I wonder if someone can point out the mistake.

here's a piece of the file:

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)

but when I typed make clean I get this:

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

or when I try to build I get this:

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

compilation terminated.

I think the issue is with this line, but I can't figure out the problem:

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

thx!

Upvotes: 1

Views: 175

Answers (3)

Chris Dodd
Chris Dodd

Reputation: 126338

You have a file that ends in .cpp, but your subst commands only deal with names that end in .c. Try:

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

Upvotes: 0

keltar
keltar

Reputation: 18409

You can split C and C++ sources into separate lists, and use pattern rules to build your sources, e.g.:

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)

Upvotes: 0

MadScientist
MadScientist

Reputation: 100916

Your sources variable is:

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

Note the filename lz4hc.cpp. Then you substitute .c with .o, that gives you a filename of lz4hc.opp which is exactly the error you see.

It's not clear if you really want a mix of C and C++ files, or if you just forgot to change the name of one of the files in the variable.

Upvotes: 2

Related Questions