Search code examples
compilationclangstdin

How can I compile a program using Clang from standard input?


I have a program coming from standard input. I would like to compile it. Is this possible and if so, how can I do this?


Solution

  • You can pipe input to the compiler with the - argument.

    To compile a simple program (replace programsource with something that outputs the program source, such as an echo or some other program generator) as an example

    programsource | clang -x c - -o a.out
    

    You'll need to specify the language using the -x option (note this option is case sensitive). If you don't you'll get an error that

    error: -E or -x required when input is from standard input
    

    You can add other command line arguments as necessary.