0
votes

this is my makefile and mu dictionary includes header, log, obj, parser, lisod.c,and i don't think i forget some file some file need to be output but it doesn't appear . my error looks like make: *** No rule to make target 'obj/logging.o', needed by 'liso_server'. Stop.

CC=gcc
CFLAGS= -g -I.
_DEPS = parse.h y.tab.h logging.h
_OBJ =logging.o parse.o y.tab.o lex.yy.o 
FLAGS = -g -Wall
PARSE_DIR = parser
LOG_DIR  = log
OBJ_DIR = obj
INCLUDE_DIR = header

DEPS = $(patsubst %, $(INCLUDE_DIR)/%, $(_DEPS))
OBJ = $(patsubst %,$(OBJ_DIR)/%, $(_OBJ))


default: all

all: liso_server

$(PARSE_DIR)/lex.yy.c: $(PARSE_DIR)/lexer.l

    flex -o $@ $^

$(PARSE_DIR)/y.tab.c: $(PARSE_DIR)/parser.y

    yacc -d $^
    mv y.tab.h $(INCLUDE_DIR)/

$(OBJ_DIR)/%.o: $(PARSER_DIR)/%.c $(LOG_DIR)/%.c $(DEPS)

      $(CC) $(FLAGS) -c -o $@ $< $(CFLAGS)

liso_server: $(OBJ)

    $(CC) -o $@ $^ $(CFLAGS)

.PHONY: clean

clean:

    rm -f $(OBJ_DIR)/*.o
    rm $(PARSE_DIR)/lex.yy.* $(PARSE_DIR)/y.tab.* $(INCLUDE_DIR)/y.tab.*
    rm liso_server
1

1 Answers

0
votes

Consider the following line...

$(OBJ_DIR)/%.o: $(PARSER_DIR)/%.c $(LOG_DIR)/%.c $(DEPS)

If $(OBJ_DIR)/%.o matches obj/logging.o then the above could be expanded to...

obj/logging.o: parser/logging.c log/logging.c

Hence, you're telling make that in order to build obj/logging.o it needs both parser/logging.c and log/logging.c. It can't find parser/logging.c, abandons this rule and, since no other suitable rule can be found, concludes that it doesn't know how to make obj/logging.o.

Instead, you could make use of makes vpath directive to tell it where to look for files that match certain patterns. In this particular case try...

vpath %.c $(PARSE_DIR):$(LOG_DIR)

$(OBJ_DIR)/%.o: %.c $(DEPS)
        $(CC) $(FLAGS) -c -o $@ $< $(CFLAGS)

Now, when make looks for logging.c it will check $(PARSER_DIR) followed by $(LOG_DIR).