为什么这个Makefile没有工作?
问题描述:
我有这个C文件:为什么这个Makefile没有工作?
// test.c
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
而且这个Makefile
OBJS=$(patsubst %.c,%.o,$(wildcard *.c))
EXEC=hello.exe
all: $(EXEC)
$(EXEC): $(OBJS)
.PHONY: all
当我做make
它不会创建我的目标hello.exe
为什么?
$ make
cc -c -o hello.o hello.c
它不与任何工作hello.exe
:
$ make hello.exe
make: Nothing to be done for 'hello.exe'.
而且hello.exe
不存在:
$ ls hello.exe
ls: cannot access 'hello.exe': No such file or directory
我使用这个版本的GNU make
$ make --version
GNU Make 4.2.1
Built for x86_64-unknown-cygwin
Copyright (C) 1988-2016 Free Software Foundation, Inc.
编辑
它实际上工作时,我命名我的可执行文件与我的任何文件相同。因此,它的工作原理与:
EXEC=hello
参见:
$ make
cc -c -o hello.o hello.c
cc hello.o -o hello
不过我不明白为什么它只能这样。
答
带有hello.exe
的版本不起作用,因为make
没有隐含规则来告诉它如何将一堆*.o
转换为单个*.exe
。
但它确实有隐含规则(至少那些由POSIX指定)转hello.c
为hello.o
,并链接到hello
。
请记住,Unix不是DOS。在Unix上时忘记.exe
。