是否有可能从管道中读取gcc?

时间:2021-12-22 09:43:58

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

我正在寻找gcc的一个选项,它会让它从标准输入中读取一个源文件,主要是因为我可以做类似的事情来生成一个像flex这样生成C代码的工具的目标文件(flex的-t选项写入生成的C到标准输出):

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

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

因为我并不真正关心生成的C文件。

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

这样的事情是存在的,还是我必须使用临时文件?

2 个解决方案

#1


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

是的,但您必须使用-x选项指定语言:

# Specify input file as stdin, language as C
flex -t lexer.l | gcc -o lexer.o -xc -

#2


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.

基本上你说文件名是 - 指定文件名是 - 是说'标准输入'的标准惯例。您还需要-c标志,这样您就不会进行链接。当gcc从标准输入读取时,你必须告诉它-x是什么语言。 -x c说它是C代码。

#1


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

是的,但您必须使用-x选项指定语言:

# Specify input file as stdin, language as C
flex -t lexer.l | gcc -o lexer.o -xc -

#2


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.

基本上你说文件名是 - 指定文件名是 - 是说'标准输入'的标准惯例。您还需要-c标志,这样您就不会进行链接。当gcc从标准输入读取时,你必须告诉它-x是什么语言。 -x c说它是C代码。