zufall
zufall

Reputation: 69

Gnu Make: Strange "Multiple targets pattern" - error

I have started a SDL2-project where I have a source file containing the main function which depends on several of my own header and object files which are in other folders. My directory structure is as follows

/SDL2   // Top-Level directory of all my SDL2 projects
->  /projectX    // My current project
    ->  main.cpp
->  /obj_sdl2_ana  // Directory of all self-made object files
    ->  ...object files
    ->  ...source files of object files
->  /include_sdl2_ana   // Directory of all self-made header files
    ->  ..header files

To compile and link main.cpp against my object and header files I have written the following Makefile

TARGET = main
FILETYPE = cpp
OBJDIR = ../obj_sdl2_ana/ # Directory with self-made object files
INCDIR = ../include_sdl2_ana/ # Directory with self-made header files
IFLAGS = -I$(INCDIR)
LFLAGS = -lSDL2 -lSDL2_image # insert all necessary libraries into it
VPATH = $(OBJDIR):$(INCDIR)
ADD_RESOURCES = common_ana

ADD_INC := $(ADD_RESOURCES:%=$(INCDIR)%.hpp) # specify header files which are prerequisites
ADD_OBJ := $(ADD_RESOURCES:%=$(OBJDIR)%.o) # specify additional object files which are prerequisites

$(TARGET): $(TARGET).o $(ADD_OBJ)
    g++ $(TARGET).o $(ADD_OBJ) -g3 -o $(TARGET) $(LFLAGS)

$(TARGET).o: $(TARGET).$(FILETYPE) $(ADD_INC)
    g++ -c $(TARGET).$(FILETYPE) $(IFLAGS) -g3 -o $(TARGET).o $(LFLAGS)

$(ADD_OBJ): $(OBJDIR)%.o: $(OBJDIR)%.cpp $(INCDIR)%.hpp
    g++ -c $< $(IFLAGS) -g3 -o $@ $(LFLAGS)

I have tested this with only one object file (common_ana.o) and the corresponding header file (common_ana.hpp) but the problem is that make is tossing a "Multiple target pattern" error at the rule

$(ADD_OBJ): $(OBJDIR)%.o: $(OBJDIR)%.cpp $(INCDIR)%.hpp
    g++ -c $< $(IFLAGS) -g3 -o $@ $(LFLAGS)

I have absolutely no clue, why this error appears. The Gnu make manual suggests that this error appears when there is a misuse of static pattern rules. But I have triple-checked my Makefile and couldn't find anything which would justify the error message of gnu make. While I'm aware of a workaround, I would be really glad if someone could give me a hint, what I have done wrong with respect to the above static pattern rule.

Upvotes: 0

Views: 147

Answers (1)

user657267
user657267

Reputation: 21040

Make is including the extra space before your comments, try something the following

# Directory with self-made object files
OBJDIR = ../obj_sdl2_ana/
# Directory with self-made header files
INCDIR = ../include_sdl2_ana/
IFLAGS = -I$(INCDIR)
# insert all necessary libraries into it
LFLAGS = -lSDL2 -lSDL2_image

Same applies to the other lines, it's best to avoid inline comments.

Upvotes: 2

Related Questions