我正试图使用Makefile构建一个C程序,但不幸的是,当我试图调用一个定义的变量时,即使在对其进行转义并尝试其他转义技术之后,我也会遇到错误。
文本文件greetings-file.txt包含字符串"Goodnight Moon!n"
这是我的代码:
F= greetings-file.txt
M= Makefile-hello
# complete the makefile as instructed in the quiz2.txt file # you will need to add dependencies after the target(s)
# as well as actions in the recipes
program: hello
@echo run hello
./hello
hello.c:
@echo make hello.c based on $F and $M
@echo "#include<stdio.h>" > hello.c
@echo "int main(){printf("`cat $F`");}" >> hello.c
hello: hello.c
@echo compile hello.c
cc -o hello hello.c
clean:
@echo cleanup hello
-rm hello hello.c
这是我的错误:
compile hello.c
cc -o hello hello.c
hello.c: In function ‘main’:
hello.c:2:21: error: expected ‘)’ before ‘Goodnight’
int main(){printf(""Goodnight Moon!
^~~~~~~~~
hello.c:2:19: warning: zero-length gnu_printf format string [-Wformat-zero-length]
int main(){printf(""Goodnight Moon!
^~
Makefile-hello:27: recipe for target 'hello' failed
make: *** [hello] Error 1
编译器消息向您显示失败的语句的第一部分,甚至可以帮助您指出错误的确切位置:
int main(){printf(""Goodnight Moon! ^~~~~~~~~
它发出的详细信息也很有帮助,描述了一个零长度的格式字符串,并指出C语法不允许在"G"中出现通用文本;晚安"出现。这两者都指向同一件事:;晚安月亮"旨在显示在字符串文字中(该文字将用作printf
的格式字符串(,但却显示了一个空字符串(""
(,后面是文本。
为什么?因为一个引号直接来自make
执行的构建源文件的命令:
一个来自文件@echo "int main(){printf("`cat $F`");}" >> hello.c
greetings-file.txt
,你说它实际上包含"Goodnight Moon!n"
问候语的末尾也出现了类似的问题。我怀疑在准备这些文件时存在一些沟通错误,但也许这都是演习的一部分。无论如何,需要去掉一组引号(或者makefile中的引号可以简单地取消引号(。
您忘记了关闭"在printf语句中