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

Go To StackoverFlow.com

56

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?

2009-06-16 20:01
by Zifre
The generated C file is good to have around if you ever need to debug that code - laalto 2009-06-16 20:07
@laalto: That's a good point, but the code that flex generates is not very human readable anyways - Zifre 2009-06-16 20:11


66

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 -
2009-06-16 20:03
by Adam Rosenfield
I figured it might be - (many other tools use it), but I couldn't find anything about it in the man page.. - Zifre 2009-06-16 20:09


16

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.

2009-06-16 20:07
by nos
I know what -c is, I just left it out for simplicity (because I have a lot of other options on flex and gcc too) - Zifre 2009-06-16 20:10
Ads