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.
A pipe is a mechanism for interprocess communication; data written to the pipe by one process can be read by another process. The data is handled in a first-in, first-out (FIFO) order. The pipe has no name; it is created for one use and both ends must be inherited from the single process which created the pipe.
gcc is a compiler like javac . You give it source files, it gives you a program. make is a build tool. It takes a file that describes how to build the files in your project based on dependencies between files, so when you change one source file, you don't have to rebuild everything (like if you used a build script).
gcc is used to compile C program. g++ can compile any . c or . cpp files but they will be treated as C++ files only.
You can use g++ both to compile programs into object modules and to link these object modules together into a single program. It looks at the names of the files you give it to determine what language they are in and what to do with them.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With