Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get GCC to read from a pipe?

I'm looking for an option to gcc that will make it read a source file from the standard input, mainly so I could do something like this to generate an object file from a tool like flex that generates C code (flex's -t option writes the generated C to the standard output):

flex -t lexer.l | gcc -o lexer.o -magic-option-here 

because I don't really care about the generated C file.

Does something like this exist, or do I have to use temporary files?

like image 524
Zifre Avatar asked Jun 16 '09 20:06

Zifre


People also ask

What is GCC pipe?

Gcc is small by today's standards and -pipe adds a bit of multi-core accessible parallel execution. But by the same token the CPU is so fast that it can create that temporary file and read it back without you even noticing. And since -pipe was never the default mode, it occasionally acts up a little.


2 Answers

Yes, but you have to specify the language using the -x option:

# Specify input file as stdin, language as C flex -t lexer.l | gcc -o lexer.o -xc - 
like image 74
Adam Rosenfield Avatar answered Sep 25 '22 01:09

Adam Rosenfield


flex -t lexer.l | gcc -x c -c -o lexer.o - 

Basically you say that the filename is -. Specifying that a filename is - is a somewhat standard convention for saying 'standard input'. You also want the -c flag so you're not doing linking. And when GCC reads from standard input, you have to tell it what language this is with -x . -x c says it's C code.

like image 42
nos Avatar answered Sep 22 '22 01:09

nos