GNU做出奇怪的行为

时间:2018-01-08 19:37:05

标签: makefile gnu-make

我无法理解(gnu)make的以下行为。这是一个重现问题的最小项目:

来源树:

project/
    foo.c
    include/
        foo.h
    main.c
    makefile

foo.c的:

#include "foo.h"

foo.h中

/* blank */

main.c中:

int main(int argc, char *argv[])
{
    return 0;
}

生成文件:

CC           = gcc
INCLUDE      = include
INCLUDE_HDRS = $(shell echo $(INCLUDE)/*.h)
SOURCES      = $(filter-out main.c, $(shell echo *.c))
HEADERS      = $(shell echo *.h)
OBJECTS      = $(patsubst %.c, %.o, $(SOURCES))

%.o: %.c $(HEADERS) $(INCLUDE_HDRS)
    $(CC) -c -o $@ $< -I./$(INCLUDE)

all: main.c $(OBJECTS)
    $(CC) $< $(OBJECTS)

clean:
    -rm -f *.o
    -rm -f a.out                              

这不会构建,产生以下错误:

~$ make
gcc    -c -o foo.o foo.c
foo.c:1:10: fatal error: foo.h: No such file or directory
 #include "foo.h"
      ^~~~~~~
compilation terminated.
make: *** [<builtin>: foo.o] Error 1

编译命令以某种方式缺少-I./include标志。但是,如果我将任何.h文件添加到项目的根目录,它构建正常。我相信,如果根目录中没有.h文件,shell函数会产生垃圾,但这怎么会影响编译命令的构建方式呢?

以防万一:这是GNU Make 4.2.1,linux上的gcc版本7.2.1。

0 个答案:

没有答案
相关问题