Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipeline echo to gcc?

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.

like image 299
Masoud Ghaderi Avatar asked Dec 27 '17 23:12

Masoud Ghaderi


People also ask

What is pipe in GCC?

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.

What is the difference between make and gcc?

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).

Can G ++ compile C code?

gcc is used to compile C program. g++ can compile any . c or . cpp files but they will be treated as C++ files only.

What does G ++ do in bash?

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.


1 Answers

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.

like image 80
ndrwnaguib Avatar answered Sep 21 '22 02:09

ndrwnaguib