Makefile 通用生成文件生成目录错误
对于虚拟C项目,我有以下目录结构Makefile 通用生成文件生成目录错误,makefile,directory,gnu-make,Makefile,Directory,Gnu Make,对于虚拟C项目,我有以下目录结构 . ├── inc │ ├── getmsg.c │ └── getmsg.h ├── makefile └── src └── main.c 我当前的通用生成文件如下所示 TARGET = main # finds all .c files, inc/getmsg.c src/main.c SOURCES := $(shell find * -name *.c) # converts all .c files to .o files, in
.
├── inc
│ ├── getmsg.c
│ └── getmsg.h
├── makefile
└── src
└── main.c
我当前的通用生成文件如下所示
TARGET = main
# finds all .c files, inc/getmsg.c src/main.c
SOURCES := $(shell find * -name *.c)
# converts all .c files to .o files, inc/getmsg.o src/main.o
OBJECTS := $(SOURCES:.c=.o)
# directories that contain .h files for gcc -I flag, inc/
HEADERS := $(dir $(shell find * -name *.h))
CC = gcc
CFLAGS = -Wall -std=c99 -iquote "$(HEADERS)"
all: $(TARGET)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(TARGET): $(OBJECTS)
$(CC) -o $@ $^
clean:
rm -rf $(shell find * -name *.o) $(TARGET)
TARGET=main
#查找所有.c文件,inc/getmsg.csrc/main.c
来源:=$(shell find*-name*.c)
#将所有.c文件转换为.o文件,inc/getmsg.o src/main.o
对象:=$(源:.c=.o)
#包含gcc-I flag,inc.h文件的目录/
标题:=$(dir$(shell find*-name*.h))
CC=gcc
CFLAGS=-Wall-std=c99-iNote“$(标题)”
全部:$(目标)
%.o:%.c
$(CC)$(CFLAGS)-c$<-o$@
$(目标):$(对象)
$(CC)-o$@$^
清洁:
rm-rf$(shell find*-name*.o)$(目标)
这一切编译都很好,但它只是将.o文件转储到与其对应的.c文件相同的目录中
我想做的是将所有对象文件放入一个构建目录。为此,我将对象更改为对象:=$(patsubst%,build/%,$(notdir$(SOURCES.c=.o))
,其中列出了文件build/getmsg.o build/main.o
。然后我将%.o
目标设置为build/%.o:%.c
但是,这将返回没有使目标为'build/getmsg.o'
的规则。因此make文件无法生成.o文件。我在这里遗漏了什么?试着改变一下
%.o: %.c
到
这就是我累的原因,它给了我一个“无规则”的错误。哎呀,错过了那部分!查看以下链接。重复可能重复的
build/%.o: %.c