管道回声到gcc? [英] pipeline echo to gcc?
问题描述
要从终端使用C调用printf("Hello!");
,请使用
To call printf("Hello!");
in C from terminal I use
echo '#include<stdio.h>
void main()
{
printf("Hello!");
}' > foo.c
,然后调用gcc foo.c
进行输出.不幸的是,流水线
and then call gcc foo.c
to make the output. Unfortunately, the pipelining
echo '#include<stdio.h>
void main()
{
printf("Hello!");
}' | gcc
没有抱怨没有输入文件.最终,我想拥有一个脚本,可以在终端中使用./script [command]
编译C command
.任何建议,将不胜感激.
fails complaining for no input file. Ultimately, I want to have a script where I can compile a C command
from terminal with ./script [command]
. Any suggestion would be appreciated.
推荐答案
是的,但是您必须使用-x
选项指定语言.使用-xc
将输入文件指定为stdin,将语言指定为C(如果您希望将其作为C ++,则使用-xc++
).因此,在您的情况下,命令将是
Yes, but you have to specify the language using the -x
option. Specify input file as stdin, language as C using -xc
(if you want it to be C++, use -xc++
). So in your case the command would be
echo '#include<stdio.h>
void main()
{
printf("Hello!");
}' | gcc -o output.o -xc -
您可以阅读有关命令行编译器参数的更多信息:请参见 调用GCC 一章.
You can read more about Command Line Compiler Arguments: see Invoking GCC chapter.
但是,就像@Basile所说的那样,避免处理C文件是不值得的. 查看他的答案以获取更多信息.
However, as @Basile says, it is not worth the effort to avoid dealing with C files. Check his answer for more information.
这篇关于管道回声到gcc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!