为什么这个Makefile不起作用?

时间:2017-09-14 14:14:31

标签: makefile gnu-make

我有这个C档:

// 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

那说我不明白为什么它只是这样运作。

1 个答案:

答案 0 :(得分:2)

hello.exe版本不起作用,因为make没有隐含规则告诉它如何将一堆*.o转换为单个*.exe

确实有隐式规则(至少是POSIX指定的规则)将hello.c转换为hello.o并链接到hello

请记住,Unix不是DOS。当你在Unix上时忘掉.exe