C 如何在Makefile中输出.txt文件?

C 如何在Makefile中输出.txt文件?,c,makefile,escaping,C,Makefile,Escaping,我试图使用Makefile构建一个C程序,但不幸的是,当我试图调用一个定义的变量时,甚至在转义它和尝试其他转义技术之后,也会出现错误 文本文件greetings-file.txt包含字符串“晚安,月亮!\n” 这是我的密码: F= greetings-file.txt M= Makefile-hello # complete the makefile as instructed in the quiz2.txt file # you will need to add dependencies

我试图使用Makefile构建一个C程序,但不幸的是,当我试图调用一个定义的变量时,甚至在转义它和尝试其他转义技术之后,也会出现错误

文本文件greetings-file.txt包含字符串
“晚安,月亮!\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

您忘记了printf语句中的“closing”

编译器消息显示了失败的语句的第一部分,甚至有助于指出错误的确切位置:

它发出的详细信息也很有用,描述了一个零长度格式字符串,并指出C语法不允许在“Goodneaght”中出现“G”的地方出现一般文本。这两个消息都指向同一个东西:文本“Goodneaght Moon!“将显示在字符串文本(该文本将用作
printf
的格式字符串)中,但会显示一个空字符串(
)并在后面显示文本

为什么?因为一个引号直接来自
make
为构建源文件而执行的命令:

另一个来自文件
greetings file.txt
,您说它包含

"Goodnight Moon!\n"

问候语的结尾也出现了类似的问题。我怀疑在准备这些文件的过程中出现了一些沟通错误,但可能这都是练习的一部分。无论如何,需要使用一组引号(或者makefile中的引号可以不加引号).

请从您的终端复制并粘贴文本,而不是它的屏幕截图。图像不可搜索。请以文本形式发布错误消息。此外,它清楚地表明您缺少一个
字符…对于这一不幸事件,我深表歉意。我包含了
字符,但我仍然从您更新的错误日志中收到错误,似乎您添加了“两次”。请尝试删除
\“cat$F\”
行中的双引号,因为
“晚安,月亮!\n”
已经有了双引号。我将其包含在不同的位置,并得到了类似的错误提示!我删除了引号,在编译hello.c cc-o hello.c/usr/lib/gcc/x86_64-linux-gnu/7/./../../x86_64-linux-gnu/Scrt1.o:函数中_start':(.text+0x20):对main'collect2:错误:ld返回1个退出状态Makefile hello:27:目标'hello'的配方失败:**[hello]错误1–@IsaacAttuah,您现在描述的错误表明源文件中引入了其他一些畸形。错误消息不足以确定具体内容,但您可能可以通过查看生成的
hello.c
文件来判断。
 int main(){printf(""Goodnight Moon!
                     ^~~~~~~~~
    @echo "int main(){printf(\"`cat $F`\");}" >> hello.c
here -----------------------------^
"Goodnight Moon!\n"