2017-02-09 127 views
0

我有一个虚拟C项目的以下目录结构。通用Makefile构建目录错误

. 
├── inc 
│   ├── getmsg.c 
│   └── getmsg.h 
├── makefile 
└── src 
    └── main.c 

我现在通用的Makefile下面,

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 [email protected] 

$(TARGET): $(OBJECTS) 
    $(CC) -o [email protected] $^ 

clean: 
    rm -rf $(shell find * -name *.o) $(TARGET) 

这一切编译罚款但它只是转储.o文件到同一目录作为其相应的.c文件。

我想要做的是将所有的目标文件放到构建目录中。为此,我将OBJECTS更改为OBJECTS := $(patsubst %,build/%,$(notdir $(SOURCES:.c=.o))),其中列出了文件build/getmsg.o build/main.o。然后我将%.o目标设置为build/%.o: %.c

然而这返回No rule to make target 'build/getmsg.o'。所以make文件无法生成.o文件。我在这里错过了什么?

+0

重复http://stackoverflow.com/questions/13552575/gnu-make-pattern-to-build-output-in-different-directory -than-src –

+0

可能的重复[GNU Make模式在不同的目录下建立输出比src](http://stackoverflow.com/questions/13552575/gnu-make-pattern-to-build-output-in-different-目录比src) –

回答

0

尝试改变

%.o: %.c 

build/%.o: %.c 
+0

这就是我累了,它给了我“无规则”的错误。 – FlamingSquirrel

+0

哎呀,错过了那部分!看看下面的链接。 http://stackoverflow.com/questions/14639794/getting-make-to-create-object-files-in-a-specific-directory – MrJagaloon