【问题标题】:makefile error: “make: *** No rule to make target …”makefile 错误:“make: *** No rule to make target …”
【发布时间】:2021-01-01 19:30:23
【问题描述】:

我正在尝试使用带有 makefile 的 GCC (linux) 来编译我的项目。 我收到以下错误

"No rule to make target 'output/src/main.o', needed by 'test'.  Stop."

这是生成文件:

COMPILE_PREX ?= 
CC = $(COMPILE_PREX)gcc

SOURCE = $(wildcard src/*.c)
OBJS = $(addprefix ./output/, $(patsubst %.c, %.o, $(SOURCE)))

INCLUDES = -I ./src
CFLAGS += -O2 -Wall -g
LDFLAGS += -lpthread

TARGET = test

$(TARGET): $(OBJS)
    $(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)

%.o: %.c
    @mkdir -p ./output
    $(CC) $(CFLAGS) $(INCLUDES) -o $(addprefix ./output/, $@) -c $< 

clean:
    rm -rf ./output

【问题讨论】:

  • 你在撒谎。 %.o: %.c 规则不会创建 $@(你承诺的),而是 ./output/$@(这是谎言)。
  • 如果要将*.o输出到./output/怎么办?
  • 将规则更改为output/%.o: src/%.c 是否有效?这也需要$(CC) .... -o $@ -c $&lt;
  • 我正在尝试,但它不起作用。
  • 好的,Stackoverflow 的下一步是投票并接受对您有帮助的答案。

标签: makefile gnu-make


【解决方案1】:

这应该可以。请注意使用mkdir $(@D) 根据需要创建输出目录。

COMPILE_PREX ?=
CC = $(COMPILE_PREX)gcc

SOURCE = $(wildcard src/*.c)
OBJS = $(addprefix output/, $(patsubst %.c, %.o, $(SOURCE)))

INCLUDES = -I src
CFLAGS += -O2 -Wall -g
LDFLAGS += -lpthread

TARGET = test

$(TARGET): $(OBJS)
    $(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)

output/%.o: %.c
    @mkdir -p $(@D)
    $(CC) $(CFLAGS) $(INCLUDES) -o $@ -c $<

clean:
    rm -rf output

make clean test在这里运行它:

rm -rf output
gcc -O2 -Wall -g -I src -o output/src/hello.o -c src/hello.c
gcc output/src/hello.o -lpthread -o test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多