Makefile组合规则不执行定义的其他规则

时间:2017-06-27 12:31:41

标签: makefile gnu-make

执行make失败,因为它无法在make文件中找到先前定义的规则。此外,“dir”规则仅在我使用“make dir”时有效,但当我将其添加到组合规则时,它会列出工作目录中的文件....

make test_tester
dir
base_types.c  dynamic_array.c  gists  hashtable.c  linked_list.c  Makefile  Makefile_OLD  object.c  object_table.c  out.txt  READ_THIS.txt  sstring.c  tester.c  tests  ttime.c
build/test_tester.o
make: build/test_tester.o: Command not found
Makefile:7: recipe for target 'test_tester' failed
make: *** [test_tester] Error 127

CC=gcc
CFLAGS=-Wall -I ../include
TEST_DIR=tests
BUILD_DIR=build

test_tester:
        dir
        $(BUILD_DIR)/test_tester.o
        $(BUILD_DIR)/tester.o
        $(BUILD_DIR)/base_types.o
        $(BUILD_DIR)/object.o
        $(BUILD_DIR)/sstring.o
        $(CC) $(CFLAGS) $(BUILD_DIR)/test_tester.o $(BUILD_DIR)/tester.o $(BUILD_DIR)/base_types.o \ 
        $(BUILD_DIR)/object.o $(BUILD_DIR)/sstring.o

$(BUILD_DIR)/test_tester.o:
        $(CC) $(CFLAGS) -c $(TEST_DIR)/test_tester.c -o $@

$(BUILD_DIR)/tester.o: tester.c
        $(CC) $(CFLAGS) -c tester.c -o $@

$(BUILD_DIR)/base_types.o: base_types.c
        $(CC) $(CFLAGS) -c base_types.c -o $@

$(BUILD_DIR)/object.o: object.c
        $(CC) $(CFLAGS) -c object.c -o $@

$(BUILD_DIR)/sstring.o: sstring.c
        $(CC) $(CFLAGS) -c sstring.c -o $@

dir: mkdir -p $(BUILD_DIR)

.PHONY: clean
clean: 
        rm -f $(BUILD_DIR)/*.o

1 个答案:

答案 0 :(得分:0)

我明白了,谢谢。

CC=gcc
CFLAGS=-Wall -I ../include
TEST_DIR=tests
BUILD_DIR=build

test_tester: dir test_tester.o tester.o base_types.o object.o sstring.o
        $(CC) $(CFLAGS) $(BUILD_DIR)/test_tester.o $(BUILD_DIR)/tester.o $(BUILD_DIR)/base_types.o $(BUILD_DIR)/object.o $(BUILD_DIR)/sstring.o -o $@

test_tester.o:
        $(CC) $(CFLAGS) -c $(TEST_DIR)/test_tester.c -o $(BUILD_DIR)/$@

tester.o: tester.c
        $(CC) $(CFLAGS) -c tester.c -o $(BUILD_DIR)/$@

base_types.o: base_types.c
        $(CC) $(CFLAGS) -c base_types.c -o $(BUILD_DIR)/$@

object.o: object.c
        $(CC) $(CFLAGS) -c object.c -o $(BUILD_DIR)/$@

sstring.o: sstring.c
        $(CC) $(CFLAGS) -c sstring.c -o $(BUILD_DIR)/$@

dir: 
        mkdir -p $(BUILD_DIR)

.PHONY: clean
clean: 
        rm -fr $(BUILD_DIR)