简体   繁体   English

Makefile隐式规则不起作用

[英]Makefile implicit rule not working

I have this makefile and i'm trying to write some test targets 我有这个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)

When running "make serial_test", I'm always getting this: 当运行“ make serial_test”时,我总是得到这个:

make: *** No rule to make target `build/serial_test.o', needed by `serial_test'. Stop.

Here's the doubt. 这是个疑问。 Why I'm receiving this error if the rule $(BUILD_DIR)/%.co exists? 如果规则$(BUILD_DIR)/%。co存在,为什么会收到此错误?

Edit 编辑

To make sure that everything else was working I've tried this(this works fine) 为了确保其他一切正常,我已经尝试过了(这个工作正常)

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 $@

To make sure that the variable BUILD_DIR was note empty i did this: 为了确保变量BUILD_DIR为空,我这样做:

$(BUILD_DIR)/serial_test.o: %.c
    $(CC) $(CFLAGS) -I$(HEADER_DIR) -I$(SOURCE_DIR) -c $< -o $@

With this code above, I got another error: 通过上面的代码,我得到了另一个错误:

make: *** No rule to make target `%.c', needed by `build/serial_test.o'. Stop.
  • Is there any chance of %.c and %.o rules are broken? %.c和%.o规则是否有可能被打破?

  • If you have any design tips just answer. 如果您有任何设计技巧,请回答。 That would be helpful too. 这也将有所帮助。

Take a hard look at your pattern rule: 仔细看看您的模式规则:

$(BUILD_DIR)/%.c.o: %.c $(HEADERS)

It matches filenames with a .co extension. 它以.co扩展名匹配文件名。 The error message indicates that you're trying to build build/serial_test.o , which doesn't match the pattern because the path doesn't end in .co . 错误消息表明您正在尝试构建build/serial_test.o ,该模式与模式不匹配,因为路径未以.co结尾。

Fix your pattern in your build rule, or your patsubst to replace .c with .co , so that the pattern rule will match. 修复您的构建规则中的模式,或者将patsubst替换为.co替换为.c ,以使模式规则匹配。

  • You're mixing pattern and suffix rules, stick with the pattern rule 您正在混合模式和后缀规则,请遵循模式规则
  • Not entirely sure what you're trying to do with TEST_OBJECTS 不确定您要使用TEST_OBJECTSTEST_OBJECTS
  • You're making every object depend on every header, use dependency generation instead 您使每个对象都依赖于每个标头,而是使用依赖项生成
  • The rule for serial_test is broken, always use $@ as the output of any given rule serial_test的规则已损坏,请始终使用$@作为任何给定规则的输出
  • Make the directories a prerequisite 使目录成为先决条件
  • Recycle the built-in recipes ( COMPILE.c , LINK.o ) 回收内置配方( COMPILE.cLINK.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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM