我有这个makefile,我试图编写一些测试目标
#This a make file of serial communication driver that i'm writing.
#serial_target would be a target to test this driver isolated.
CC = gcc
DISPLAY_STRING = -DDISPLAY_STRING
LFLAGS =
CFLAGS = $(DISPLAY_STRING)
#mains macros
BUILD_DIR =build
SOURCE_DIR =src
HEADER_DIR =src
BIN_DIR =bin
SOURCES = $(wildcard $(SOURCE_DIR)/*.c)
OBJECTS = $(patsubst $(SOURCE_DIR)/%.c, $(BUILD_DIR)/%.o, $(SOURCES))
HEADERS = $(patsubst $(SOURCE_DIR)/%.c, $(HEADER_DIR)/%.h, $(SOURCES))
#serial_tests
TEST_DIR=src/test
TEST_TARGET=serial_test
TEST_SOURCES = $(wildcard $(TEST_DIR)/*_test.c) \
$(SOURCES)
TEST_OBJECTS = $(filter %.o ,$(patsubst %.c, $(BUILD_DIR)/%.o, $(subst /, , $(TEST_SOURCES))))
TEST_HEADERS = $(TEST_SOURCES:.c=.h)
all:
serial_test: $(TEST_OBJECTS)
$(CC) $(CFLAGS) -I$(SOURCE_DIR) -I$(HEADER_DIR) -o $(BIN_DIR)/$@ $^
$(BUILD_DIR)/%.c.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -I$(HEADER_DIR) -I$(SOURCE_DIR) -c $< -o $@
setup:
mkdir $(BUILD_DIR)
mkdir $(BIN_DIR)
clean:
rm -rf $(EXEC) $(OBJECTS)
在运行&#34; make serial_test&#34;时,我总是得到这个:
make: *** No rule to make target `build/serial_test.o', needed by `serial_test'. Stop.
这是疑问。如果规则$(BUILD_DIR)/%。c.o存在,为什么我收到此错误?
修改
为了确保其他一切正常,我已经尝试过了(这很好)
serial_test: $(TEST_OBJECTS)
$(CC) $(CFLAGS) -I$(SOURCE_DIR) -I$(HEADER_DIR) -o $(BIN_DIR)/$@ $^
build/serial_test.o: src/test/serial_test.c
$(CC) $(CFLAGS) -I$(HEADER_DIR) -I$(SOURCE_DIR) -c $< -o $@
build/serialCom_xsens.o: src/serialCom_xsens.c
$(CC) $(CFLAGS) -I$(HEADER_DIR) -I$(SOURCE_DIR) -c $< -o $@
为了确保变量BUILD_DIR是空的,我做了这个:
$(BUILD_DIR)/serial_test.o: %.c
$(CC) $(CFLAGS) -I$(HEADER_DIR) -I$(SOURCE_DIR) -c $< -o $@
使用上面的代码,我又遇到了另一个错误:
make: *** No rule to make target `%.c', needed by `build/serial_test.o'. Stop.
%.c和%.o规则是否有可能被破坏?
如果您有任何设计提示,请回答。这也会有所帮助。
答案 0 :(得分:1)
仔细查看您的模式规则:
$(BUILD_DIR)/%.c.o: %.c $(HEADERS)
它将文件名与.c.o
扩展名匹配。该错误消息表明您正在尝试构建build/serial_test.o
,这与该模式不匹配,因为该路径未在.c.o
中结束。
在您的构建规则中修复您的模式,或使用patsubst
将.c
替换为.c.o
,以便模式规则匹配。
答案 1 :(得分:0)
TEST_OBJECTS
serial_test
的规则已被破坏,始终使用$@
作为任何指定规则的输出COMPILE.c
,LINK.o
)display_string := -Ddisplay_string
source_dir := src
header_dir := src
build_dir := build
bin_dir := bin
sources := $(wildcard $(source_dir)/*.c)
objects := $(sources:$(source_dir)/%.c=$(build_dir)/%.o)
test_dir := src/test
test_target := serial_test
test_sources := $(wildcard $(test_dir)/*_test.c)
test_objects := $(test_sources:$(test_dir)/%.c=$(build_dir)/%.o) $(objects)
deps := $(test_objects:%.o=%.d)
CPPFLAGS := $(display_string) -I$(header_dir) -MMD -MP
$(bin_dir)/serial_test: $(test_objects) | $(bin_dir)
$(LINK.o) $^ $(LDLIBS) -o $@
$(build_dir)/%.o: $(source_dir)/%.c | $(build_dir)
$(COMPILE.c) $(OUTPUT_OPTION) $<
$(build_dir) $(bin_dir): ; mkdir $@
clean: ; $(RM) serial_test $(objects) $(deps)
-include $(deps)