简体   繁体   English

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

[英]How do I use 'make' in C in case of a filename which contains a space?

Introduction: I made a file called temperature 2.c and when I use the terminal to 'make' the file, it returns an error: 简介:我制作了一个名为Temperature 2.c的文件,当我使用终端“制作”该文件时,它返回一个错误:
make: *** No rule to make target 'temperature 2'. Stop.
Terminal output screenshot 终端输出屏幕截图

This is what I type into the terminal window. 这是我在终端窗口中键入的内容。
make temperature\\ 2

Version Information: I'm using macOS 10.12.5 with Apple LLVM version 8.1.0 (clang-802.0.42) 版本信息:我将macOS 10.12.5与Apple LLVM版本8.1.0(clang-802.0.42)
Xcode Version 8.3.2 (8E2002) Xcode版本8.3.2(8E2002)

Additional Information: I've tried using make "temperature 2" as someone suggested that it works on Windows but it's not working on Mac. 附加信息:我曾尝试使用make "temperature 2"因为有人建议它可以在Windows上运行,但不能在Mac上运行。
I have selected the correct directory in the terminal window and I'm able to compile all other files flawlessly. 我已经在终端窗口中选择了正确的目录,并且能够完美地编译所有其他文件。
Screenshot of the file 该文件的屏幕截图

I'm aware of the fact that I can simply replace the space with an underscore to fix the issue but I want to know why this is happening in the first place. 我知道一个事实,我可以简单地用下划线替换空格以解决此问题,但我想知道为什么会首先发生这种情况。
Terminal works fine handling other directories which have a space in their name (using a backslash), so why isn't it working in this case? 终端可以很好地处理名称中带有空格的其他目录(使用反斜杠),那么为什么在这种情况下它不起作用?

Thanks in advance. 提前致谢。

Make comes with several built in rules, but they won't be designed to cope with spaces or other "unusual" characters in the file names, so you'll need to write a Makefile that overrides them or change your filenames. Make带有几个内置规则,但是它们不能用来处理文件名中的空格或其他“不寻常”字符,因此您需要编写一个覆盖它们或更改文件名的Makefile。

This rule is the one you're using - you have to put quotes around the target file $@ and input file $< . 这是您正在使用的规则-您必须在目标文件$@周围加上引号,并在输入文件$<周围加上引号。

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

When calling make, it requires you to have a Makefile, for quick and simple one file c program, 调用make时,它要求您拥有一个Makefile,以便快速,简单地使用一个文件c程序,

try 尝试

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

or 要么

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

Your make clearly tells you that with: 您的品牌清楚地告诉您:

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

Make feeds the commands to a shell that executes them as commands. Make将命令提供给执行命令的外壳程序。 If you have one line like this: 如果您有这样一行:

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

when you try to do 当你尝试去做

make "some thing".o

it will try to find a file called "some thing.c" and will execute 它将尝试找到一个名为“ some something.c”的文件并执行

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

which is to try to compile to some the three files thing.o , some and thing.c 试图thing.o三个文件编译为thing.osomething.c

But if you change the rule to 但是,如果您将规则更改为

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

it will feed the shell with this 它将以此喂食外壳

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

that will produce the correct behaviour. 会产生正确的行为。

Makefile 生成文件

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

Sample run (in a FreeBSD system): 样本运行(在FreeBSD系统中):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM