我该如何使用' make'在C中,如果文件名包含空格?

时间:2017-05-25 09:09:03

标签: c makefile terminal clang

简介:我创建了一个名为 temperature 2.c 的文件,当我使用终端'make'文件时,它会返回错误:
make: *** No rule to make target 'temperature 2'. Stop.
Terminal output screenshot

这是我在终端窗口中键入的内容。
make temperature\ 2

版本信息:我使用macOS 10.12.5与 Apple LLVM版本8.1.0(clang-802.0.42)
Xcode版本8.3.2 (8E2002)

其他信息:我已尝试使用make "temperature 2",因为有人建议它可以在Windows上运行,但它无法在Mac上运行。
我在终端窗口中选择了正确的目录,并且能够完美地编译所有其他文件。
Screenshot of the file

我知道我可以简单地用下划线替换 space 以解决问题,但我想知道为什么会发生这种情况。
终端可以很好地处理其名称中有空格的其他目录(使用反斜杠),那么为什么它不能在这种情况下工作呢?

提前致谢。

3 个答案:

答案 0 :(得分:5)

Make附带了几个内置规则,但它们不会被设计为处理文件名中的空格或其他“异常”字符,因此您需要编写一个覆盖它们或更改文件名的Makefile。

此规则是您正在使用的规则 - 您必须在目标文件$@周围加上引号并输入文件$<

%: %.c
        $(CC) $(CFLAGS) -o "$@" "$<"

答案 1 :(得分:2)

调用make时,它需要你有一个Makefile,用于快速简单的一个文件c程序,

尝试

gcc -o "temperature 2" "temperature 2.c"

clang -o "temperature 2" "temperature 2.c"

你的作品清楚地告诉你:

make: *** No rule to make target 'temperature 2'.  Stop.

答案 2 :(得分:0)

将命令提供给shell,将命令作为命令执行。如果你有这样一行:

.c.o:
    $(CC) $(CFLAGS) -c -o $@ $<

当你尝试

make "some thing".o

它会尝试找到一个名为&#34; some thing.c&#34;的文件。并将执行

cc  -c -o some thing.o some thing.c

尝试编译为三个文件thing.osomething.c

但是如果您将规则更改为

.c.o:
    $(CC) $(CFLAGS) -c -o "$@" "$<"

它将使用此

提供shell
cc  -c -o "some thing.o" "some thing.c"

将产生正确的行为。

生成文件

.c.o:
    $(CC) $(CFLAGS) -c -o "$@" "$<"

示例运行(在FreeBSD系统中):

$ make "the problem.o"
cc -O -pipe -c -o "the problem.o" "the problem.c"