Issue
To call printf("Hello!");
in C from terminal I use
echo '#include<stdio.h>
void main()
{
printf("Hello!");
}' > foo.c
and then call gcc foo.c
to make the output. Unfortunately, the pipelining
echo '#include<stdio.h>
void main()
{
printf("Hello!");
}' | gcc
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.
Solution
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 -
You can read more about Command Line Compiler Arguments: see Invoking GCC chapter.
However, as @Basile says, it is not worth the effort to avoid dealing with C files. Check his answer for more information.
Answered By - Andrew Naguib