make:模式规则没有应用?

时间:2012-01-29 20:38:54

标签: c makefile

跟进:Set up a development environment on Linux targeting Linux and Windows

项目目录如下:

/pps
    /src
    /obj
    /bin
    Makefile

Makefile的内容是:

OBJ_DIR = obj
SRC_DIR = src
BIN_DIR = bin

INCLUDE = -I./$(SRC_DIR)
LIBS = 

_SRCS = print_current_dir.c test_main.c

_OBJS = print_current_dir.o test_main.o

SRCS = $(addprefix $(SRC_DIR)/,$(_SRCS))
OBJS = $(addprefix $(OBJ_DIR)/,$(_OBJS))

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

all: $(BIN_DIR)/pps-linux $(BIN_DIR)/pps-win32

$(BIN_DIR)/pps-linux: $(OBJS)
    CC = cc
    CFLAGS = -g -Wall $(INCLUDE) $(LIBS)

$(BIN_DIR)/pps-win32: $(OBJS)
    CC = i586-mingw32msvc-cc
    CFLAGS = -g -Wall $(INCLUDE) $(LIBS)

.PHONY: clean

clean:
    rm -f $(OBJS) $(SRC_DIR)/~ core $(BIN_DIR)/*

文件print_current_dir.c和test_main.c仅用于测试。

此makefile不起作用:

$ make
make: *** No rule to make target `obj/print_current_dir.o', needed by `bin/pps-linux'.  Stop.

我认为问题出在%.o规则上。我确信这是微不足道的,但我对make不是很有经验。

谢谢。

1 个答案:

答案 0 :(得分:3)

这条规则:

$(OBJ_DIR)/%.o: %.c %.h

指定obj/foo.o取决于foo.c,而不是src/foo.c。没有foo.c,因此无法应用此规则(因此错误消息)。

这应该做的工作:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/%.h
相关问题